1

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?

Community
  • 1
  • 1
VoidKing
  • 6,282
  • 9
  • 49
  • 81

1 Answers1

1

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.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • Hi, Mike. Thanks for shedding some light on this for me. I'm trying this now. I noticed that you have `path="/Error404.cshtml"`, do I not want that to be `path="~/Error404.cshtml"` (notice the tilde)? I ask because I noticed that the tilde was included in other urls in the web.config file, and while I could test this myself, I thought you might want to know in case you wanted to change that in your answer (if it is, indeed, even relevant). – VoidKing Sep 11 '13 at 14:07
  • Okay, so I tried this and it totally worked (thank you, again for that!), but I just wanted to clarify a couple of things for my education's sake, if you don't mind. Firstly, though, I just want to say (concerning my comment above), that the tilde, included here, should definitely be excluded, as it will not generate a valid path if the error occurs. Now, first question: "I used subStatusCode="-1" in the error for statusCode 500. This works but wanted to make sure that was right, since I'm not sure what a subStatusCode even is." And for the second question:...(continued) – VoidKing Sep 11 '13 at 14:33
  • (continued)..."You stated that this was all necessary because of my use of 'extension-less' urls, but the urls generated from the C# file included the file extension (example: /someDir/somePic.jpg). Am I misunderstanding what you mean by 'extension-less'?" – VoidKing Sep 11 '13 at 14:36
  • I assumed that you had problems getting your custom page to display when you requested a cshtml file without its extension. IIS sees that as a missing directory. However, for missing static files (.html, .jpg, .txt etc) the solution is the same - to set the custom error page in the `system.webServer` section to prevent IIS default behaviour. – Mike Brind Sep 11 '13 at 18:36
  • Okay, I see, well you certainly solved my problem. So there's nothing wrong with using `subStatusCode="-1"` for statusCode 500? – VoidKing Sep 11 '13 at 22:01
  • It's the default subStatusCode value for IIS, so it's fine, and probably actually not needed. Removing it will make no difference. – Mike Brind Sep 12 '13 at 04:55