Exception Handling in ASP.NET MVC Web Application

 

ASP.NET MVC Error handling using CSharp:

Exception handling is crucial part of every application. It is required for logging errors into database or text/log file, very importantly presenting them to the user in a nicely in case of exception.

Introduction:

When I was working on application I encounter exceptions (e.g. 400, 500, etc ) but I was not sure of what might have caused it. That's where I started exploring various approaches of exception handling in ASP.NET MVC.

There are various ways to handle exceptions, based upon usability and error location/type (SQL, Application, etc)

We are going to see all of them -I will keep on adding stuff to this article :)

It seems then, that this is a good exercise in understanding a number of things:

 

Exception Handling in ASP.NET MVC - At Page/View Level - Using Action Method

 

 

Exception Handling in ASP.NET MVC - At Application Level - Using Global.aspx

 

Using Code/ How It Works:

Exception Handling in ASP.NET MVC - At Page/View Level - Using Action Method

 

This typical way of handling exceptions, using try...catch block inside action method as follows -

 
 public ActionResult Index() {
 try {
 //To do - code block processing
 } catch (Exception e) {
 //Redirect to an error page
 Response.Redirect("URL to the error page");
 }
 return View();
 }
 

When return type of action is JSON you should return a JSON with the exception message and handle it on the client side, as follows-

 
 public JsonResult GetJson() {
 try {
 //To do - code block processing
 } catch (Exception e) {
 //Return the exception message as JSON
 return Json(new {
 error = e.Message
 });
 }
 return Json("Return data here");
 }
 
  • Advantage - custom error handling can be done for particular action method.

  • Disadvantage- We need to repeat code (try… catch block) in every action (for each view/page).

 

 

Exception Handling in ASP.NET MVC - At Application Level - Using Global.aspx

 

 

In previous approach we need to repeat code, that's why it is good to handle exceptions at application level, so that will write code once use it for all pages.

In order to do that you have to add Application_Error() function definition in Global.aspx as follows, this is just an example.

 
 protected void Application_Error() {
 Exception unhandledException = Server.GetLastError();
 int httpCode = 0;
 // Handle HTTP errors
 if (unhandledException.GetType() == typeof (HttpException)) {
 HttpException httpException = unhandledExceptionas HttpException;
 if (httpException == null) {
 Exception innerException = unhandledException.InnerException;
 httpException = innerException as HttpException;
 }
 if (httpException != null) {
 httpCode = httpException.GetHttpCode();
 switch (httpCode) {
 case (int) HttpStatusCode.Unauthorized:
 Response.Redirect("~/Error/ErrorUnauthorized");
 break;
 case 404:
 Response.Redirect("~/Error/ErrorNotFound");
 break;
 default:
 Response.Redirect("~/Error/Default");
 break;
 }
 }
 }
 // Log the exception and notify system operators
 // You can create Class file e.g. Exception utility to log error details either in database or text file
 //ExceptionUtility.LogException(unhandledException, Request.Url.ToString(), httpCode);
 // Clear the error from the server
 Server.ClearError();
 }

 

 

Thank You...Please like this article on social site if this worked for you… Coding is fun !!! Isn't it...