First, the outset
Fiter is often used in Web APIs, mainly for logging, security verification, global error handling, etc. the Web API provides the basic type of two types of filters: Actionfilterattribute,exceptionfilterattribute ; Two classes are abstract classes, actionfilter main implementations before executing the request method body (overriding the base class method OnActionExecuting), and subsequent event handling (overriding the base class method onactionexecuted) Exceptionfilter mainly implements the triggering exception method (overriding the base class method Onexception).
Before writing the API to use the log4net, need to add a log in each controller or method of the method, and later learned that there is a filter this east, is written in the filter is very convenient (this is not so-called aspect-oriented programming AOP).
Second, the use of Exceptionfiter 2.1, a new class to implement the Exceptionfilter class, overriding the Onexception method.
2.2. Use in the controller
2.3. Effect
Third, the use of Exceptionfilterattribute 3.1, a new class implementation Exceptionfilterattribute
3.2, in the need to use the Controller or action before adding features can
Use of filters in the C # Web API