Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Exception or Error Handling and Logging in MVC4

Exception or Error Handling and Logging in MVC4

01 Apr 2024
Intermediate
41K Views
8 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with Web API Foundations Course - Free

Exception or Error Handling: An Overview

Error handling is the main concern in any application, whether it is a web application or a desktop application. Usually, we catch the exception and log its details to a database or text, XML file and also display a user-friendly message to the end-user in place of the error. In this MVC Tutorial, we will explore more about theException or Error Handling which will include the HandleError filter in mvc, customize error handling in mvc4, how to handle and log errors in mvc, and log exceptions in mvc. Consider our ASP.NET MVC Course for a better understanding of all MVC core concepts.

Asp.Net MVC has some built-in exception filters. HandleError is the default built-in exception filter. Let's see how to use this filter within your application.

HandleError Attribute

The HandleErrorAttribute filter works only when custom errors are enabled in the Web.config file of your application. You can enable custom errors by adding a customErrors attribute inside the <system.web> node, as shown below:


</system.web>
...
<customErrors mode="On" defaultRedirect="Error.htm"/>
</system.web>
  

HandleError Attribute can be used to handle errors at Action Method level, Controller level and Global level.

HandleError Attribute at Action Method Level

Simply put this attribute to the action method tells MVC how to react when an exception occurs.


[HandleError(ExceptionType = typeof(System.Data.DataException), View = "DatabaseError")]
public ActionResult Index(int id)
{
 var db = new MyDataContext();
 return View("Index", db.Categories.Single(x => x.Id == id));
}

In the above example when a database exception (System.Data.DataException) occurs during the execution of the Index action, MVC will display the DatabaseError view.

HandleError Attribute at Controller Level

Simply put this attribute to the controller tells MVC how to react when an exception occurs within the action methods of this controller.

[HandleError(ExceptionType = typeof(System.Data.DataException), View = "DatabaseError")]
public class HomeController : Controller
{
 /* Controller Actions with HandleError applied to them */
}

In the above example when a database exception (System.Data.DataException) occurs during the execution of the controller's any action method, MVC will display the DatabaseError view.

HandleError Attribute at Global Level

You can also apply the HandleError Attribute for the entire application by registering it as a global error handler. For registering a global error handler, open the FilterConfig.cs file within the App_Start folder to find the RegisterGlobalFilters method.

By default, the ASP.NET MVC template has already registered a global HandleErrorAttribute to the GlobalFilterCollection for your application. Here you can also add your own custom filter to the global filter collection as well.

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
 filters.Add(new HandleErrorAttribute
 {
 ExceptionType = typeof(System.Data.DataException),
 View = "DatabaseError"
 });

 filters.Add(new HandleErrorAttribute()); //by default added
}

Note

Always remember, by default, global filters are executed in their registered order. so register error filters for specific exception types before any other.

You can also set the order of execution of these filters by giving a number of values to each. The above-registered filters can be re-written as:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
 filters.Add(new HandleErrorAttribute(),2); //by default added
 filters.Add(new HandleErrorAttribute
 {
 ExceptionType = typeof(System.Data.DataException),
 View = "DatabaseError"
 },1);
} 

Now, all the filters will be executed in the number sequence.

Limitation of HandleError Attribute

  1. It has no support to log the exceptions since it suppresses the error once it is handled.

  2. It only catches 500 HTTP errors and doesn't catch other HTTP errors like 404,401 etc.

  3. It doesn't catch the errors that occur outside the controllers like errors that occur in the model.

  4. It returns an error view even if an error occurred in AJAX calls that is not useful on the client side. It would be great to return a piece of JSON in AJAX exceptions.

Extending HandleError Filter for logging and handling more errors

You can also make your custom error filter by extending HandleErrorthe filter. Let's see how to extend this filter to log the exceptions and return a JSON object for AJAX calls.

public class CustomHandleErrorAttribute : HandleErrorAttribute
{
 public override void OnException(ExceptionContext filterContext)
 {
 if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
 {
 return;
 }
 
 if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500)
 {
 return;
 }
 
 if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
 {
 return;
 }
 
 // if the request is AJAX return JSON else view.
 if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
 {
 filterContext.Result = new JsonResult 
 { 
 JsonRequestBehavior = JsonRequestBehavior.AllowGet, 
 Data = new
 { 
 error = true,
 message = filterContext.Exception.Message
 } 
 };
 }
 else
 {
 var controllerName = (string)filterContext.RouteData.Values["controller"];
 var actionName = (string)filterContext.RouteData.Values["action"];
 var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
 
 filterContext.Result = new ViewResult
 {
 ViewName = View,
 MasterName = Master,
 ViewData = new ViewDataDictionary(model),
 TempData = filterContext.Controller.TempData
 };
 }
 
 // log the error by using your own method
 LogError(filterContext.Exception.Message, filterContext.Exception);
 
 filterContext.ExceptionHandled = true;
 filterContext.HttpContext.Response.Clear();
 filterContext.HttpContext.Response.StatusCode = 500;
 
 filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
 }
}
Conclusion

So in this article, we have learned about the Exception or Error Handling and Logging in MVC4. I hope you enjoyed learning these concepts while programming with Asp.Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Level up your career in MVC with our ASP.Net Core Certification

FAQs

Logging and exception handling are like two peas in a pod. When a problem happens in your Java code, that typically means you have an exception that needs to be handled, and of course, any time an error or unanticipated event occurs, that occurrence should be logged appropriately

Logging is your lens into what's happening during execution, providing insights for both development and operations teams.

Exception handling is a mechanism that separates code that detects and handles exceptional circumstances from the rest of your program.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this