How can I use existingResponse=“Auto” successfully?

后端 未结 2 1585
心在旅途
心在旅途 2020-12-30 23:28

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

相关标签:
2条回答
  • 2020-12-31 00:08

    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:

    • set existingResponse="Auto" in web.config;
    • set TrySkipIisCustomErrors = true in your action (one that returns 4xx status and a content);
    • clear server error in global.asax (in 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.

    0 讨论(0)
  • 2020-12-31 00:26

    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;
    }
    
    0 讨论(0)
提交回复
热议问题