Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS7 Overrides customErrors when setting Response.StatusCode?

Tags:

asp.net

iis-7

Having a weird problem here. Everybody knows that if you use web.config's customErrors section to make a custom error page, that you should set your Response.StatusCode to whatever is appropriate. For example, if I make a custom 404 page and name it 404.aspx, I could put <% Response.StatusCode = 404 %> in the contents in order to make it have a true 404 status header.

Follow me so far? Good. Now try to do this on IIS7. I cannot get it to work, period. If Response.StatusCode is set in the custom error page, IIS7 seems to override the custom error page completely, and shows its own status page (if you have one configured.)

Has anyone else seen this behavior and also maybe know how to work around it? It was working under IIS6, so I don't know why things changed.

Note: This is not the same as the issue in ASP.NET Custom 404 Returning 200 OK Instead of 404 Not Found

like image 806
Nicholas Head Avatar asked Jan 12 '09 02:01

Nicholas Head


People also ask

Where do you put customErrors mode off?

The customErrors tag must be placed within tags. Create a <customErrors> tag within a "web. config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

How do I turn off custom error mode?

Choose the ASP.NET tab. Click on "Edit Configuration". Click the Custom Errors tab. Select Off for custom error mode.

What is customErrors mode RemoteOnly?

RemoteOnly will give end users the custom error message and local usrs the standard asp.net error page. If your developers use a local web server for development you have both in one. Another approach is to set the <customErrors> to Off on development servers and set it to On in the production environment.


2 Answers

Set existingResponse to PassThrough in system.webServer/httpErrors section:

  <system.webServer>     <httpErrors existingResponse="PassThrough" />   </system.webServer> 

Default value of existingResponse property is Auto:

Auto tells custom error module to do the right thing. Actual error text seen by clients will be affected depending on value of fTrySkipCustomErrors returned in IHttpResponse::GetStatus call. When fTrySkipCustomErrors is set to true, custom error module will let the response pass through but if it is set to false, custom errors module replaces text with its own text.

More information: What to expect from IIS7 custom error module

like image 96
Pavel Chuchuva Avatar answered Sep 21 '22 04:09

Pavel Chuchuva


The easiest way to make the behavior consistent is to clear the error and use Response.TrySkipIisCustomErrors and set it to true. This will override the IIS global error page handling from within your page or the global error handler in Application_Error.

Server.ClearError(); Response.TrySkipIisCustomErrors = true; 

Typically you should do this in your Application_Error handler that handles all errors that your application error handlers are not catching.

More detailed info can be found in this blog post: http://www.west-wind.com/weblog/posts/745738.aspx

like image 26
Rick Strahl Avatar answered Sep 21 '22 04:09

Rick Strahl