Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an exception into HTTP 404 response in the Application_Error

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.

like image 783
Dmytrii Nagirniak Avatar asked Nov 05 '22 13:11

Dmytrii Nagirniak


1 Answers

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.

like image 126
Andrej Golcov Avatar answered Nov 15 '22 07:11

Andrej Golcov