Use of filters in the C # Web API

Source: Internet
Author: User
Tags types of filters
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

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.