IIS7 Overrides customErrors when setting Response.StatusCode?
回答1
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
Even though, we had HttpContext.Current.Response.TrySkipIisCustomErrors = true; but that didn't work. But following this solved my issue.
回答2
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
Response.TrySkipIisCustomErrors not working
Well - this is a first. First time I managed to find the answer before hitting the submit button :)
Found this IIS.net article: http://www.iis.net/configreference/system.webserver/httperrors which then lead me to find this: http://msdn.microsoft.com/en-us/library/ms690576(v=vs.90).aspx
Scroll down to the part that explains what the options for existingResponse
mean. I had mine set to Replace
which means it ignores TrySkipIisCustomErrors
completely. Changed it to Auto
and it's working.
Thanks for answering your own question, your link pointed me to the right solution: <httpErrors existingResponse="PassThrough" />