Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Custom Error Handling Application_Error Global.asax?

I have some basic code to determine errors in my MVC application. Currently in my project I have a controller called Error with action methods HTTPError404(), HTTPError500(), and General(). They all accept a string parameter error. Using or modifying the code below. What is the best/proper way to pass the data to the Error controller for processing? I would like to have a solution as robust as possible.

protected void Application_Error(object sender, EventArgs e) {     Exception exception = Server.GetLastError();     Response.Clear();      HttpException httpException = exception as HttpException;     if (httpException != null)     {         RouteData routeData = new RouteData();         routeData.Values.Add("controller", "Error");         switch (httpException.GetHttpCode())         {             case 404:                 // page not found                 routeData.Values.Add("action", "HttpError404");                 break;             case 500:                 // server error                 routeData.Values.Add("action", "HttpError500");                 break;             default:                 routeData.Values.Add("action", "General");                 break;         }         routeData.Values.Add("error", exception);         // clear error on server         Server.ClearError();          // at this point how to properly pass route data to error controller?     } } 
like image 679
aherrick Avatar asked Jul 23 '09 11:07

aherrick


People also ask

How to Handle exception in global asax in MVC?

Setting HandleError Attribute as a Global Filter Any unhandled exception that takes place within the boundary of the MVC application will now be handled by this global error handler. To test this global handler, comment out the [HandleError] attribute from the action or the controller and then run the application.

How to Handle application error in global asax?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web.

What is global ASAX in MVC?

The Global. asax file is a special file that contains event handlers for ASP.NET application lifecycle events. The route table is created during the Application Start event. The file in Listing 1 contains the default Global. asax file for an ASP.NET MVC application.


1 Answers

Instead of creating a new route for that, you could just redirect to your controller/action and pass the information via querystring. For instance:

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 = "HttpError404";         break;       case 500:         // server error         action = "HttpError500";         break;       default:         action = "General";         break;       }        // clear error on server       Server.ClearError();        Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));     } 

Then your controller will receive whatever you want:

// GET: /Error/HttpError404 public ActionResult HttpError404(string message) {    return View("SomeView", message); } 

There are some tradeoffs with your approach. Be very very careful with looping in this kind of error handling. Other thing is that since you are going through the asp.net pipeline to handle a 404, you will create a session object for all those hits. This can be an issue (performance) for heavily used systems.

like image 151
andrecarlucci Avatar answered Sep 20 '22 17:09

andrecarlucci