asp.net MVC Note Action filter

Source: Internet
Author: User
Keywords Filter nbsp execute CAN implement
Tags .net access added asp asp.net cache code control

The action filter is embodied in the Code as a property added to the action, and the MVC framework contains filters such as:

outputcache– instructs the controller to cache the returned results within a specified time.

handleerror– handles exceptions thrown by action in Contrller

authorize– constrain the access of a specific user or role to an action

Using the action filter

The action filter can control not only individual action, but also the entire contrller. At the same time, one action can apply multiple filters. Like what:

public class Datacontroller:controller
{
[OutputCache (duration=10)]
public string Index ()
{
Return DateTime.Now.ToString ("T");
}
}


This action returns the current time, but if you refresh the interface within 10 seconds, you will always get the same value, because OutputCache (duration=10) is used here.

Type of filter

The ASP.net MVC Framework supports the following types of filters:
1. Authorization Filter-implements the Iauthorizationfilter interface. This type of filter is used to implement user authentication and access authorization to the action. For example, authorize belongs to the authorization filter.
2. Action Filter-implements the Iactionfilter interface. It can contain some logic before or after the action is executed, for example, some filters are specifically used to modify the data returned by the action.
3. Result filter-implements the Iresultfilter interface. It can contain some logic before the view result is generated or generated, such as some filters designed to modify the results of a view before it is presented to the browser.
4. Exception Filter-implements the Iexceptionfilter interface. It is used to process action or result errors, or to log errors.
The filter's default order of execution is the same as the list above, for example, the authorization filter executes before the action filter, and the exception filter is always executed at the end. Of course, you can also set the order of the filter execution according to your needs.

Continue >> Next [1th] [page 2nd]
Related Article

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.