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? } }
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With