I am using the web.config code below to redirect requests to missing pages to a 404 error handling page:
<customErrors mode="On" defaultRedirect="404.aspx" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="404.aspx"/> </customErrors>
It works fine when I look for pages such as “missing.aspx” but it does not work for pages without the “.aspx” extension such as “missing.asp” or just “missing”. When it does not work, it just loads a standard IIS 7.5 error page.
What am I doing wrong? I am using .net 4. I noticed other people asking the same question but they didn’t get an answer.
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
As dbaseman states this is because the asp.net handlers are not called for non-asp.net files. An easy way to force the asp.net handler to operate on all requests is to set the following in your web.config.
<system.webServer> <modules runAllManagedModulesForAllRequests="true"> </system.webServer>
This tells IIS to run through all of the managed modules for all requests such as .html, .jpg, .css, .js, etc. This is typically frowned upon as it just introduces extra processing and latency.
Another option to try (cleaner than the above) was posted as an answer here: https://stackoverflow.com/a/6661699/701062
Method 2
The reason is that non-ASPX extensions never make it to the ASP.NET handler; those errors you see are coming from IIS. There is a separate section httpErrors under system.webServer in web.config that you will need to configure to handle these errors. See here for more info.
Example from the link:
<configuration> <system.webServer> <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" > <remove statusCode="500" /> <error statusCode="500" prefixLanguageFilePath="C:ContosoContenterrors" path="500.htm" /> </httpErrors> </system.webServer> </configuration>
Method 3
Add the following to the Web.config:
<system.webServer>
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL" >
<remove statusCode="404" />
<error statusCode="404" path="/Default.aspx" responseMode="Redirect" />
</httpErrors>
</system.webServer>
I took Osprey’s code, and added responseMode=”Redirect” to fix the problem of just displaying the source code of the page.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0