Question

I have seen in multiple places

[here] http://www.localwisdom.com/blog/2010/08/how-to-setup-custom-404s-for-iis-and-asp-net-through-web-config/

[and from a previous question here] How can I make a custom error page in ASP.NET web-pages with WebMatrix?

how to setup the web.config file to force custom error pages for given status codes (I think the only ones I care about are "500" and "404").

I am using this as the <system.web> element in the web.config XML.

<system.web>
    <compilation debug="true" targetFramework="4.0"><assemblies><add assembly="System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /></assemblies></compilation>
    <customErrors mode="On">
        <error statusCode="500" redirect="~/Error.cshtml" />
        <error statusCode="404" redirect="~/Error404.cshtml" />
    </customErrors>
</system.web>

The custom error page for the status code, "500" serves up just fine on normal server-side errors, but for some reason, no matter what I do, I can't seem to get the custom error page for status code "404" to show up if, say, a broken link were clicked on the page. Instead, it just shows the normal "404, resource not found" default page.

I have also tried to change the order of the <error> tags (not that that should help, but as you can see, I am out of reasonable things to try, LOL).

Am I misunderstanding how this whole thing works or am I simply getting the configuration/syntax wrong in some way?

--------------------------------UPDATE----------------------------------------

Okay, so apparently, my custom 404 page works if a normal link is broken, however, I have .cs file with certain html generating methods in it.

On a single secondary page, this will be it's entire contents:

@Html.Raw(ContentGenerator.Generate("CityCouncil"))

The values for what to display on the page come from a database, are encoded as they are added to the string, and then written to the page. If the C# string contains a broken link, then the default error page does not show up. Why should this make a difference? My guess is, it's a timing issue, and that by the time C# writes the HTML to the page, the web.config file can no longer influence this link or something.

Is there any way to still achieve a default error page for these links?

Was it helpful?

Solution

The customErrors entry in your web.config under system.web works for urls with file extensions as we have established. but to work for extension-less urls, you need to override IIS by adding an entry to the system.webServer node in your web.config:

<system.webServer>
    <httpErrors existingResponse="Replace" errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath=""
               path="/Error404.cshtml" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>

Having done this, you also need to move any other custom error handling to the system.webServer section and remove the customErrors entry under system.web.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top