Convert an exception into HTTP 404 response in the Application_Error

馋奶兔 提交于 2020-01-14 09:46:06

问题


First of all, quickly what exactly I want to achieve: translate particular exception into the HTTP 404 so the ASP.NET can handle it further.

I am handling exceptions in the ASP.NET (MVC2) this way:

    protected void Application_Error(object sender, EventArgs e) {
        var err = Server.GetLastError();
        if (err == null)
            return;
        err = err.GetBaseException();

        var noObject = err as ObjectNotFoundException;
        if (noObject != null)
            HandleObjectNotFound();

        var handled = noObject != null;
        if (!handled)
            Logger.Fatal("Unhandled exception has occured in application.", err);
    }

    private void HandleObjectNotFound() {
        Server.ClearError();
        Response.Clear();
        // new HttpExcepton(404, "Not Found"); // Throw or not to throw?
        Response.StatusCode = 404;
        Response.StatusDescription = "Not Found";
        Response.StatusDescription = "Not Found";
        Response.Write("The whole HTML body explaining whata 404 is??");
    }

The problem is that I cannot configure default customErrors to work with it. When it is on then it never redirects to the page specified in customErrors: <error statusCode="404" redirect="404.html"/>.

I also tried to raise new HttpExcepton(404, "Not Found") from the handler but then the response code is 200 which I don't understand why.

So the questions are:

  1. What is the proper way of translating AnException into HTTP 404 response?
  2. How does customErrors section work when handling exceptions in Application_Error?
  3. Why throwing HttpException(404) renders (blank) page with success (200) status?

Thanks,
Dmitriy.


回答1:


In a few words, you if you manually set HTTP status in Application_Error, you lose possibility to use customErrors section handler, since you call Server.ClearError().

Handle the exception before Application_Error or derive the exception from HttpException.

  • What is the proper way of translating AnException into HTTP 404 response?

It's better to handle exceptions in Controller. You can introduce base class controller and handle most of exceptions in custom HandleError attribute. You can throw HttpException their and it will be properly handled by customErrors section handler.

You can also derive your ObjectNotFoundException exception from HttpException(404)

Application_Error is the last chance to handle an exception. You have only Response API to handle it. You can manually set status code and write to response or manually trigger redirect to custom error page or call Server.TransferRequest() to existing html or aspx file (not to the controller action). In current asp.net version, you cannot set or change Server.GetLastError in Application_Error method only retrieve or clear it.

  • How does customErrors section work when handling exceptions in Application_Error?

By calling Server.ClearError() you also clean current request error therefore it is not handled by customErrors section handler

  • Why throwing HttpException(404) renders (blank) page with success (200) status?

You should no throw any exception in Application_Error method. Exception means that your error handling failed.



来源:https://stackoverflow.com/questions/2808121/convert-an-exception-into-http-404-response-in-the-application-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!