How can I use existingResponse="Auto" successfully?

asp.net-mvc, c#, iis

Solution

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;
}

Problem

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 all failures, I only want to expose them when I have custom responses. Auto, is set by default, but I deliberately set it. However, the documentation says "SetStatus" flag must be set, but I have no idea how to do such a thing. I wrote the following four controller methods in order to test it, and only BadRequestD works. The others set the status code and the status just fine, but the body content is "Bad Request". ``` public ActionResult BadRequestA() { Response.StatusCode = 400; return Content("weeeeee"); } public ActionResult BadRequestB() { Response.Status = "400 U DUN MESSED UP"; return Content("weeeeee"); } public ActionResult BadRequestC() { Response.Status = "400 U DUN MESSED UP"; Response.StatusCode = 400; return Content("weeeeee"); } public ActionResult BadRequestD() { Response.StatusCode = 400; Response.TrySkipIisCustomErrors = true; return Content("weeeeee"); } ```

Original source