So I am returning detailed 400 error responses from my MVC web app. Setting existingResponse=\"PassThrough\" works, but that\'s not what I want. I don\'t want to expose al
If you need to have custom responses with 4xx http statuses and still want to use Custom Error Pages here's what you should do:
existingResponse="Auto"
in web.config;TrySkipIisCustomErrors = true
in your action (one that returns 4xx status and a content);Application_Error()
- Server.ClearError()
) and re-set the status code (Reponse.StatusCode = ((HttpException)Server.GetLastError()).GetHttpCode()
)It's weird that IIS team didn't implement existingResponse
attribute for specific status codes, so it's impossible to use existingResponse="PassThrough"
just for one (or few) codes.
However, the documentation says "SetStatus" flag must be set, but I have no idea how to do such a thing
It's actually talking about the fTrySkipCustomErrors
flag/argument to the IHttpResponse::SetStatus
method in the IIS C++ SDK (see note I added to bottom of documentation here). But in ASP.NET the flag is exposed as Response.TrySkipIisCustomErrors
. So according to:
http://www.iis.net/configreference/system.webserver/httperrors
Auto = Leaves the response untouched only if the SetStatus flag is set
I would expect to see IIS replace the response with its own html error page content (you can configure what that content is) by default unless you set:
Response.TrySkipIisCustomErrors = true;
Which is what you're seeing.
Additional related info, in MVC5 it seems to act as if that flag is true even if it's false for uncaught exceptions which I don't see in WebForms. As a workaround in Global.asax I'm:
protected void Application_Error()
{
var error = Server.GetLastError();
Server.ClearError();
//code to log error here
var httpException = error as HttpException;
Response.StatusCode = httpException != null ? httpException.GetHttpCode() : (int)HttpStatusCode.InternalServerError;
}