Introduction:-

In this article, we will learn about the HandleError filter and discuss about the exception handling mechanisms that will fit to an MVC3 application. Exception handling is a serious issue in any application.  Whether it's web or desktop. Implementing an appropriate exception handling is compulsory in every application. ASP.NET MVC3 comes with some built-in support for exception handling through exception filters. The HandleError attribute is the default built-in exception filter.

[HandleError] Attribute:-

[HandleError] attributes that can be applied over an action or a controller or even at a global level. When you create a MVC3 application you will see the HandleErrorAttribute is added to the GlobalFiltersCollection in the Global.asax.cs which under the Global.asax.

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
      filters.Add(new HandleErrorAttribute());
}

Working of HandleError Attribute:-

The HandleError attribute handles the exceptions that are raised by the controller


actions, filters and views, it will return a custom view named Error.cshtml which is


placed in the Shared folder. The HandleError filter works only if


the <customErrors>section is turned On in web.config under the <system.web> tag.


<system.web>
                …………………………
                <customErrors mode="On"/>
</system.web>


Note:- HandleError Filter handles the exception while customErrors turn On in Web.config file  by default customErrors isReadonly

Now write that code in HomeController.
    public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }
        [HandleError(ExceptionType = typeof(DivideByZeroException), Master = "_Layout", View = "Error")]  
        public ActionResult About()
        {
            string str;
            object obj = null;
            str = obj.ToString();
            return View();
        }
   [HandleError] attribute have four properties:-


1.       ExceptionType:- The ExceptionType allows you to specify what


exception that attribute should handle


2.       Master: - The Master allows you to manage which master page (or as


Razor refers to them, Layout) you desire to render.


3.       Order: -


4.       View: - The View allows you to specify which error view (page) you want


it to redirect to.


[HandleError(ExceptionType = typeof(DivideByZeroException), Master = "_Layout", Order = 1, View = "Error")]

       

 

This code will Generate the runtime exception “Object reference not set to an


instance of an object” when execute the About () action. [HandleError] attribute will


handle the error and call the Error.cshtml View which is under the Shared


Folder or Home Folder (created by user View).

Exception Handling in ASP.NET MVC3

In Error.cshtml:-
@model System.Web.Mvc.HandleErrorInfo
@{
    ViewBag.Title = "Error"; }
<h2>Error</h2>
<h2>
    Sorry, an error occurred as processing your request.
</h2>

 Now Run That Project and select the About Menu:-

Exception Handling in ASP.NET MVC3

After Run and select the About Menu you will see on URL bar call the “Home/About” but here is render  the Error.cshtml  View that the reason is  raises Runtime Exceptions.

  Modified On Nov-28-2017 08:54:50 AM

Leave Comment