ASP. net mvc Framework Action Filters

Source: Internet
Author: User

ASP. net mvc Framework includes several action filters:

    • Outputcache-caches the output of controller actions within the specified time.
    • Handleerror-handling of controller action errors.
    • Authorize-Restrict access permissions through specified users or roles.

 

Different types of filters

ASP. net mvc Framework supports four different types of filters:

    1. Authorization Filters-ImplementationIauthorizationfilter interface attributes.
    2. Action Filters-ImplementationIactionfilter interface attributes.
    3. Result Filters-ImplementationIresultfilter interface attributes.
    4. Exception Filters-ImplementationIexceptionfilter InterfaceAttribute.

Filters are executed in the order listed above. For example, the authorization filter is always executed at the beginning, and the exception filter is always executed at the end.

Authorization filter is used to verify and authorize the Controller action. For example, authorize filter is an example of filter verification;

Action filter contains logic used before or after the action is executed. For example, you can use an action filter to modify the View data returned by an action;

Result filter contains some logic for the view result of the action before and after execution. For example, you can modify a view result before the view is displayed in the browser;

Exception action is used to handle exception information. You can also use the exception filter to record error logs.

These filter types are executed according to the specified sequence. to control their execution sequence, you need to set the filter order attribute.

The base classes of these classes are system. web. MVC. filterattribute class. to implement a specific filter type, you can create a class that inherits the class and implement one or more iauthorization, iactionfilter, iresultfilter, and predictionfilter interfaces.

 

Actionfilterattribute base class

To make it easier for users to create a custom action filter, ASP. net mvc Framework provides a base class of actionfilterattribute, which implements the iactionfilter and iresultfilter interfaces and inherits the filter class.

The terms here are not exactly the same. Technically, this class inherits actionfitlerattribute and implements the action filter and result filter interfaces at the same time. However, in a loose sense, Asp. in the. net mvc Framework, any type that implements the filter is action filter.

The actionfilterattribute class can be rewritten using the following methods:

    • onactionexecuting-called before the Controller action is executed
    • onactionexecuted-called after the Controller action is executed
    • onresultexecuting-called before the Controller action result is executed
    • onresultexecuted-called after the Controller action result is executed

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.