MVC and Web API filters Filter

Source: Internet
Author: User
Tags types of filters

MVC and Web API filter (filter)

ASP. NET MVC supports the following types of action filters:

· Authorization filters. These filters are used to implement iauthorizationfilter and make security decisions about whether to perform an action method, such as a property that performs an authentication or validation request. The Authorizeattribute class and the Requirehttpsattribute class are examples of authorization filters. The authorization filter runs before any other filters.

· Action filter. These filters are used to implement iactionfilter and the wrapper action method execution. The Iactionfilter interface declares two methods: OnActionExecuting and onactionexecuted. OnActionExecuting run before the action method. OnActionExecuted run after the action method, you can perform additional processing, such as providing additional data to the action method, checking the return value, or canceling the action method.

· The result filter. These filters are used to implement Iresultfilter and wrapper ActionResult objects. Iresultfilter declares two methods: Onresultexecuting and onresultexecuted. Onresultexecuting runs before executing the ActionResult object. Onresultexecuted runs after the result and can perform additional processing on the result, such as modifying the HTTP response. The Outputcacheattribute class is an example of a result filter.

· The exception filter. These filters are used to implement iexceptionfilter and are executed when an unhandled exception is thrown during ASP. NET MVC pipeline execution. Exception filters can be used to perform tasks such as logging or displaying error pages. The Handleerrorattribute class is an example of an exception filter.

Different types of filters

The ASP. NET MVC Framework supports four different types of filters:

    1. Authorization Filter-The implemented IAuthorizationFilter property.
    2. Action Filter-The implemented IActionFilter property.
    3. Result Filter-The implemented IResultFilter property.
    4. Exception Filter-The implemented IExceptionFilter property.

Special attention:

The filter in MVC is System.web.Mvc.dll implementation

Implement global filters: In the FilterConfig.cs file under the App_start folder

public static void Registerglobalfilters (Globalfiltercollection filters)

{

The instance object of the new Test_mvc.mvc_filter () filter class

Filters. ADD (New Test_mvc.mvc_filter ());//--Register Global Filter

}

In the Web Api filter System.web.http.dll in the System.web.http.Filters implementation, add controller must note: Be sure to select an empty API controller. If you choose an empty MVC controller then the filter fails for that controller.

Implementing a global Filter: Global.asax file:

protected void Application_Start ()

{

The instance object of the new Test_http.http_filter () filter class

GLOBALCONFIGURATION.CONFIGURATION.FILTERS.ADD (Newtest_http.http_filter ());//--Register Global Filter

}

Attention:

[CSharp]View PlainCopy
    1. Using System.Net.Http;
    2. Actioncontext.response = ActionContext.Request.CreateResponse (Httpstatuscode.ok, T value);

MVC and Web API filters Filter

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.