murday1983
murday1983

Reputation: 4016

ASP.Net - Cant Get My Custom Error Page Working

I have an asp.net website and i am rying to configure a 404 page but i cant seem to get it working.

The code i have in my web.config is

<system.web>
  <customErrors mode="On" defaultRedirect="ResponseRewrite">
    <error statusCode="404" redirect="~/Page_Not_Found.aspx" />
  </customErrors>
</system.web>
<system.webServer>
     <httpErrors errorMode="Custom">
          <remove statusCode="404"/>
          <error statusCode="404" path="~/Page_Not_Found.aspx" responseMode="Redirect"/>
     </httpErrors>
</system.webServer>

Error page HTML

<%@ Page Title="- Page not found" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Page_Not_Found.aspx.cs" Inherits="WebApplication1.Page_Not_Found" %>

<asp:Content ID="404BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <div class="col-xs-12 col-sm-9">
        <h1>Page not found</h1>
        <p>Use this area to provide additional information.</p>
        <p>Use this area to provide additional information.</p>
        <p>Use this area to provide additional information.</p>
        <p>Use this area to provide additional information.</p>
        <p>Use this area to provide additional information.</p>
        <p>Use this area to provide additional information.</p>
    </div>
</asp:Content>

When I enter and invalid URL i get the below server error

Server Error in '/' Application. Runtime Error Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

What am I doing wrong?

I have looked at other solutions on here and also on the web but all the fixes dont seem to help me.

Upvotes: 1

Views: 2486

Answers (4)

murday1983
murday1983

Reputation: 4016

Finally found out what my issue was. It was an issue i had with the ID in my asp:Content, I had added 404 to it (displayed in my inital post), if i removed it or changed it to alphacharacters it works

Was

<asp:Content ID="404BodyContent" ContentPlaceHolderID="MainContent" runat="server">

Fix - Removed

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

Fix - Alpha characters

<asp:Content ID="FourOFourBodyContent" ContentPlaceHolderID="MainContent" runat="server">

Upvotes: 0

Peter
Peter

Reputation: 14508

I suspect that the page you want to show as a 404 page generates an exception. This doesn't necessarily mean the exception is in the page itself, it could also come from the Master page the page uses.

You can try setting a breakpoint in your global asax:

protected void Application_Error()
{
    var error = Server.GetLastError();
    Logger.Log(error); //This is my own Logging library, so implement something yourself, but you get the idea.
}

When this code fires, you can examine the inner exception details and see what is happening.

Upvotes: 1

Anicho
Anicho

Reputation: 2667

There are two things you need to do.

1) Turn off custom errors to get to the bottom of what's actually causing the issue.

2) It seems in IIS8 there are issues with using '~' tilda in the path for custom error page please try the following:

<system.web>
  <customErrors mode="On" defaultRedirect="ResponseRewrite">
    <error statusCode="404" redirect="/Page_Not_Found.aspx" />
  </customErrors>
</system.web>

<system.webServer>
  <httpErrors errorMode="Custom">
    <remove statusCode="404"/>
    <error statusCode="404" path="/Page_Not_Found.aspx" responseMode="Redirect"/>
 </httpErrors>
</system.webServer>

http://forums.asp.net/t/1938152.aspx?Tilde+character+not+recognized+in+IIS8+for+custom+error+pages+in+web+config

Also its worth mentioning don't use both configuration entries if you are IIS7+ then ensure you use httpErrors over customErrors.

References:

Asp.Net Forums - Tilda Not Recognised

Stackoverflow - Difference between httpErrors and customErrors

Upvotes: 0

Ga&#235;tan
Ga&#235;tan

Reputation: 103

Might be an Exception thrown that is not caught in your application (internal server error)

Try adding this :

<error statusCode="500" redirect="https://www.google.fr/"/>

And check if it's redirecting you on Google. If so, this is an application code related issue and not because of the IIS.

Also defaultRedirect="ResponseRewrite" seems incorrect to me. Shouldn't it be in redirectMode="ResponseRewrite" attribute?

Default redirect is the page that IIS is redirecting if error is not part of the detailed custom error (404 in your case).

If your app throw an 500 error code, you'll get redirected on ResponseRewrite, which the IIS may not find

Upvotes: 1

Related Questions