in Education by
What are the methods of handling an Error in MVC?

1 Answer

0 votes
by

Exception handling may be required in any application, whether it is a web application or a Windows Forms application.

 

ASP.Net MVC has an attribute called "HandleError" that provides built-in exception filters. The HandleError attribute in ASP.NET MVC can be applied over the action method as well as Controller or at the global level. The HandleError attribute is the default implementation of IExceptionFilter. When we create a MVC application, the HandleError attribute is added within the Global.asax.cs file and registered in the Application_Start event.

  1. public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
  2. {  
  3.     filters.Add(new HandleErrorAttribute());  
  4. }  
  5. protected void Application_Start()  
  6. {  
  7.     AreaRegistration.RegisterAllAreas();  
  8.     RegisterGlobalFilters(GlobalFilters.Filters);  
  9.     RegisterRoutes(RouteTable.Routes);  
  10. }  

Important properties of HandleError attribute

 

The HandleError Error attribute has a couple for properties that are very useful in handling the exception.

 
ExceptionType
 
Type of exception to be catch. If this property is not specified then the HandleError filter handles all exceptions.
 
View
 
Name of the view page for displaying the exception information.
 
Master
 
Master View for displaying the exception.
 
Order
 
Order in which the action filters are executed. The Order property has an integer value and it specifies the priority from 1 to any positive integer value. 1 means highest priority and the greater the value of the integer is, the lower is the priority of the filter.
 
AllowMultiple
 
It indicates whether more than one instance of the error filter attribute can be specified.
 

Example

  1. [HandleError(View = "Error")]  
  2. public class HomeController: Controller  
  3. {  
  4.     public ActionResult Index()  
  5.     {  
  6.         ViewBag.Message = "Welcome to ASP.NET MVC!";  
  7.         int u = Convert.ToInt32(""); // Error line  
  8.         return View();  
  9.     }  
  10. }  

HandleError Attribute at Action Method Level,

  1. [HandleError(View = "Error")]  
  2. public ActionResult Index()  
  3. {  
  4.     ViewBag.Message = "Welcome to ASP.NET MVC!";  
  5.     int u = Convert.ToInt32(""); // Error line  
  6.     return View();  
  7.  

Related questions

0 votes
    What are the Folders in MVC application solutions?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
0 votes
    What is difference between MVC and Web Forms?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
    What is Database First Approach in MVC using Entity Framework?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
0 votes
    What is Bundling and Minification in MVC?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
0 votes
    What is Razor View Engine in MVC?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
0 votes
    Explain the concept of MVC Scaffolding?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
    Explain the need of display mode in MVC?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
0 votes
    How do you implement Forms authentication in MVC?...
asked Apr 5, 2021 in Education by JackTerrance
...