S
S
SvetlyiAkaPro2014-02-25 22:36:07
ASP.NET
SvetlyiAkaPro, 2014-02-25 22:36:07

Error handling in Asp.net MVC?

There is such an error handler in Global.asax.cs

protected void Application_Error(object sender, EventArgs e)
{
  Exception exception = Server.GetLastError();
  Response.Clear();

  HttpException httpException = exception as HttpException;

  if (httpException != null)
  {
    string action;

    switch (httpException.GetHttpCode())
    {
      case 404:
        // page not found
        action = "HttpError";
        break;
      default:
        action = "HttpError";
        break;
    }
    Session["error"]=exception.Message;
    // clear error on server
    Server.ClearError();

    Response.Redirect(String.Format("~/Home/HttpError/?message={0}", exception.Message));
  }
}

After calling a 404 error anywhere, execution aborts with a message that an error has occurred. When I click Continue, my error page appears and everything is fine. If you just enter the left address, then everything is normally intercepted, my error page appears. How to be? Why is the error not normally caught?
throw new HttpException(404, "not found");

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander S, 2014-02-26
@SvetlyiAkaPro

When I click Continue, my error page appears and everything is fine .

In both cases, everything is fine with you, but something is already not normal in the question. Perhaps the condition of the problem is not quite correctly formulated.
Perhaps you mean that if you explicitly throw an exception, the studio throws you into the code, and then only shows the page with an error, and if the path is incorrectly specified, the page with the error is immediately shown. Is there a breakpoint in the handler method?
If so, then everything is simple: in the first case, you explicitly throw an exception in the controller's own implementation, that is, you have access to the code where it fell out. In the second case, roughly speaking, compiled DLLs of the engine do this, to the code of which you do not have access to the studio and therefore the exception inside immediately goes to the global handler.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question