Action filters and the order in which custom OutputCache ActionFilterAttribute events occur

Source: Internet
Author: User
Tags filter current time set time types of filters

Understanding Action Filters

Action filter is an attribute that can be applied to the controller action-or the entire controller, whose base class is System.Web.Mvc.FilterAttribute. It defines how the action executes. The ASP.net MVC framework contains several action filters.

Handleerror– This action filter handles errors that occur when controller action executes.

Outputcache– This action filter caches the output of controller action for a set time.

authorize– This action filter allows you to restrict access to specific users or roles.

Use Action Filter

Action filter is an attribute. You can apply most of the action filters to a single controller action or an entire controller.

For example, the following data controller has an index () method that returns the current time. This action has OutputCache action filter. This filter causes the value returned by the action to be cached for 10 seconds.

The settings used by the VaryByParam property do not recommend using all parameters to differentiate by setting the value of "*". This can cause a cache overflow.

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www. codehighlighter.com/
    
-->public class Datacontroller:controller {////Get
        :/data/
        [ OutputCache (Duration = 20,varybyparam = "")] public
        string Index ()
        {return
            DateTime.Now.ToString ();
        }
    }

If you repeatedly call the index () action (constantly refreshing the current page), you will see that the current content is unchanged in duration = 20 seconds.

A single action Filter–outputcache action filter– is applied to the index () method. Again, you can apply multiple action filters on the same action.

Different types of filters

The ASP.net MVC framework supports many different types of filters:

Authorization filters– implements Iauthorizationfilter characteristics.

The Action filters– implements the Iactionfilter feature.

Result filters– implement Iresultfilter characteristics.

Exception filters– implements Iexceptionfilter characteristics.

Filters are executed in the order listed above. For example, authorization filters always executes before action filters, exception filters after all other types of 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.