What is the difference between the customErrors
and httpErrors
sections of the web.config file in ASP.NET MVC applications?
What are the guidelines for using each section?
What is the difference between the customErrors
and httpErrors
sections of the web.config file in ASP.NET MVC applications?
What are the guidelines for using each section?
Disclaimer: This is from my experience and not proven fact.
Both are used to define error handling for a website, but different software refers to different config elements.
customErrors
are a legacy (backwards compatable) element, used by Visual Studio Development Server (aka. VSDS or Cassini).httpErrors
are the new element which is only used by IIS7.This highlights the possible problem when developing ASP.NET websites while using VSDS instead of the local IIS.
Also, refer to this post by myself about how to handle error messages with IIS7, if you wish to have full control of the error output.
Summary:
VSDS
- usecustomErrors
IIS6
- usecustomErrors
IIS7
- usehttpErrors
.and if you develop with
VSDS
but publish toIIS7
, then i guess u'll need both.<customErrors>
versus<httpErrors>
<customErrors>
<httpErrors>
Quoted source: Custom 404 and error pages in ASP.NET (excellent article)
ExecuteURL
serves dynamic content such as an .aspx page (thepath
value has to be a server relative URL):File
serves a custom error file, such as a .html page:Reference: HTTP Errors (www.iis.net)
for more details, read the www.iis.net link above
Errors section in web config is for providing custom http error handling approach there are two section, one customErrors inside the section system.web and another httpErrors inside the section system.webServer (as given below)
customErrors : This section was in use before IIS 7 introduced, IIS 6 5 and before fully use this section for handling custom http errors according to http status code.
httpErrors : IIS 7 and later use this section as well as customErrors section to handle custom http errors based on their file extensions if requested page extension register with ISAPI dll (.aspx, ashx, .asmx, .svc etc) like index.aspx then IIS pick up setting from customeErrors section else it pick up setting from httpErrors (IIS 7 hosted mode must be set as integrated mood not classic)
below are the examples that is for 404 error handling check link :
httperrors vs customerrors in webconfig , iis, asp.net
*Updated April 2016
The customErrors attribute is used when the .net code is throwing an exception (404, 403, 500 etc) and the httpErrors attribute is used when IIS itself is throwing an exception.
There are a lot of pitfalls trying to configure this correctly. So if you are looking for a quick example, the best 2 options you have are:
Example 1: Using html pages
Example 2: using aspx pages
And in the aspx error pages you need to do something like this (example 404 page):
Note: Using extension less urls in the customErrors section is not possible!. (without hacks)
One work around is to disable custom errors and let http errors handle the custom page. A friend has created such setup, when I find some time, I will share the code.
Background
A good custom error page will:
So to clarify some options in our config:
<customErrors mode="RemoteOnly"
. You can specify here:On
,Off
,RemoteOnly
.On
= Always show custom error pagesOff
= Always show the real errorRemoteOnly
= Show the error locally, but show the custom error page remotely. So we wantRemoteOnly
for statement 1<customErrors redirectMode="ResponseRewrite"
. You can specify here:ResponseRedirect
orResponseRewrite
. TheResponseRedirect
mode will redirect the error page to the custom error page. For a link crawler (SEO), this will result in 302 -> 500, but you want the link crawler to get a 500 error.<httpErrors errorMode="DetailedLocalOnly"
. This the equivalent of thecustomErrors
mode. Options that you have:Custom
,Detailed
,DetailedLocalOnly
.A good blog post which helped me a lot is: http://benfoster.io/blog/aspnet-mvc-custom-error-pages