The Exceptionfilter filter for ASP.

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.