This article is mainly for you to introduce the ASP. NET Core MVC Global Filter Exceptionfilter filter, with a certain reference value, interested in small partners can refer to
This department class will explain the use of the built-in global filters in ASP. NET core MVC, which will be divided into the following chapters
Exceptionfilter filter for ASP. NET Core MVC filter (i)
ASP. NET Core MVC filter Actionfilter filter (ii)
ASP. NET Core MVC filter Resultfilter filter (iii)
Resourcefilter filter for ASP. NET Core MVC filter (iv)
ASP. NET Core MVC filter Authorizationfilter filter (v)
Brief introduction
The exception filter, as its name implies, is the filter that is used when the program has an exception. Used for processing when an uncaught exception occurs on the system.
Implement a custom exception filter
Customizing a global exception filter requires implementing the Iexceptionfilter interface
public class Httpglobalexceptionfilter:iexceptionfilter {public void Onexception (Exceptioncontext context) { throw new notimplementedexception (); } }
The Iexceptionfilter interface will require the implementation of the Onexception method, which is triggered when an uncaught exception occurs on the system. The Onexception method has a Exceptioncontext exception context, which contains specific exception information, HttpContext, and MVC routing information. Once the system has an uncaught exception, the most common practice is to use the log tool, the details of the exception is recorded, to facilitate the correction of debugging. The following is a log record implementation.
<summary>//Global exception Filter///</summary> public class Httpglobalexceptionfilter:iexceptionfilter { ReadOnly Iloggerfactory _loggerfactory; ReadOnly ihostingenvironment _env; Public Httpglobalexceptionfilter (Iloggerfactory loggerfactory, ihostingenvironment env) {_loggerfactory = LoggerF Actory; _env = env; The public void Onexception (Exceptioncontext context) {var logger = _loggerfactory.createlogger (context). Exception.TargetSite.ReflectedType); Logger. LogError (new EventId (context). Exception.hresult), context. Exception, context. Exception.Message); var json = new Errorresponse ("Unknown error, please retry"); if (_env. Isdevelopment ()) JSON. Developermessage = context. Exception; Context. Result = new Applicationerrorresult (JSON); Context. HttpContext.Response.StatusCode = (int) httpstatuscode.internalservererror; Context. Exceptionhandled = true; }public class Applicationerrorresult:objectresult {Public Applicationerrorresult (object value): Base (value) {StatusCode = (int) httpstatuscode.internalservererror; }}public class Errorresponse {public errorresponse (string msg) {Message = msg; public string Message {get; set;} Public object Developermessage {get; set;} }
Registering a global filter
The filter has been written, and then it needs to be registered in ASP. NET core MVC. Locate the system root Startup.cs file and modify the Configureservices method as follows
Services. ADDMVC (options = options. Filters.add
Test
Throws an exception in the request
The log captures the exception information correctly
The browser returns a 500 error and returns a custom error message.