ASP.NET: How can I properly redirect requests with 404 errors?
I'd like my ASP.NET MVC application to redirect failed requests to matching action methods of 开发者_C百科a certain controller. This works fine on my development machine running Windows 7, but not on my production machine running Windows 2008 R2.
I set up my web.config as follows:
<customErrors mode="On" defaultRedirect="/Error/ServerError/500">
<error statusCode="403" redirect="/Error/AccessDenied" />
<error statusCode="404" redirect="/Error/FileNotFound" />
</customErrors>
This customErrors section works fine on both of my machines (production and development) for 500 Internal Server errors.
It also works fine for 404 errors on my development machine.
However, it does not properly redirect 404 errors on the production machine. Instead of /Error/FileNotFound, I get the standard 404 page that comes with IIS 7.
What could be the problem here?
See What is the difference between customErrors and httpErrors?
Also see http://msdn.microsoft.com/en-us/library/ms690576.aspx
customErrors pertains to ASP.Net. httpErrors is new to IIS7 ( which you are using in both instances I'm assuming ).
The problem is most likely related you your IIS Pipeline mode. Maybe your production might be running in IIS Classic Pipeline Mode?
EDIT
It's the other way around. In Integrated Pipeline mode, the customErrors tag is not used.
In case someone else has the same problem: The solution is to add a httpErrors element to the system.webServer section:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/Error/FileNotFound" responseMode="ExecuteURL" />
</httpErrors>
In Windows 2008 R2, this can also be done via the IIS control panel (see "Error Pages").
精彩评论