Exception Handling Comprehensive
ASP.NET MVC provides various Exception Handling techniques at controller level, action level and global level to control the flow of our application execution. All possible handling techiques are discussed here. Recommendation:Recommended best practice is to use the global Application_Error() event to log all the exceptions along with <customErrors> element in web.config to redirect it to appropriate pages.
- Global Error Handler
- Try Catch Finally
- Override OnException()
- Using [HandleError] Attribute
- Inheriting from “HandleErrorAttribute”
- Handling HTTP errors
Global Error Handler
Global Error Handling code in Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
//...
}
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
// form authentication code
//...
}
protected void Application_Error(object sender, EventArgs e)
{
// get the error
Exception exception = Server.GetLastError();
// log or email error
//...
// clear the error, to convey to ASP.NET that the exception has been handled and there is no longer an exception in the application
Server.ClearError();
// redirect to a common error display page
Response.Redirect("/Home/Error");
}
}
Handling HTTP errors
All MVC exception handling techniques do not handle HTTP errors such as file not found, ans 500 error’s, and so on. In order to do handle these errors, we need to implement the error and an error status code in web.config file as shown.Handle Http Errors in Web.Config
<system.web>
<customErrors mode="On" defaultRedirect="~/error.htm">
<error statusCode="403" redirect="~/403.htm"/> <!-- no access -->
<error statusCode="404" redirect="~/404.htm"/> <!-- file not found -->
<error statusCode="500" redirect="~/500.htm"/> <!-- internal server error -->
</customErrors>
</system.web>
Try Catch Finally
This is the most simple way to capture and handle an exception.Override OnException()
You can override the “OnException” event of the controller. A recommened way to handle this way is to use a BaseController() or ControllerBase() class which will be used for global inheritance.Using [HandleError] Attribute
Inheriting from “HandleErrorAttribute”
Ginger CMS
the future of cms, a simple and intuitive content management system ... ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ... CFTurbine
cf prototyping engine, generates boilerplate code and views ... Search Engine LITE
create your own custom search engine for your web site ... JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ... Validation Library
complete validation library for your web forms ...
the future of cms, a simple and intuitive content management system ... ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ... CFTurbine
cf prototyping engine, generates boilerplate code and views ... Search Engine LITE
create your own custom search engine for your web site ... JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ... Validation Library
complete validation library for your web forms ...