This article is mainly for you to introduce the MVC exception processing related information, with a certain reference value, interested in small partners can refer to
In the daily development, we will go to catch a lot of anomalies, to deal with, usually our method is to be in need of exception processing place with a try catch block, but, if there is a lot of exception processing, then, will frequently write the try catch block, for our natural ' lazy ' Programmer, always want to find a shortcut. So, there will be global exception handling, so today we'll look at how to do global exception handling in MVC.
First, the MVC framework's own global exception handling
In MVC, the framework has given us a set of attribute class Handleerrorattribute classes for global exception handling. We can find such a line of code in the FilterConfig.cs file in the App_start folder in MVC
public static void Registerglobalfilters (Globalfiltercollection filters) { filters. ADD (New Handleerrorattribute ()); }
This is the instantiation of a Handleerrorattribute class, which is placed in the filter. And then our views>. There is a error.cshtml page in the shared folder, where the model type of the page is System.Web.Mvc.HandleErrorInfo, and this is what the MVC framework has written for me, which we can use directly.
In the Error.cshtml page, we can do further processing to display the error message and to display the error message on demand. These error messages are found in some of the properties in the System.Web.Mvc.HandleErrorInfo class.
For example: the error.cshtml.
In control, we deliberately write an exception:
public class Homecontroller:controller {public actionresult Index () { String i = "12a"; Int J = Convert.ToInt32 (i); return View (); } }
Run, let's look at the results.