ASP. net mvc filter (4)

Source: Internet
Author: User
Tags types of filters

ASP. net mvc filter (4) Preface

The previous article roughly describes the execution process of the IActionFilter method in the framework. This article will introduce the use of IActionFilter filters.

 

ASP. NET MVC Filter
  • Overall object model of the filter in the system framework
  • IAuthorizationFilterAuthorization authentication filter Execution Process
  • UseIAuthorizationFilterFilter
  • IActionFilterExecution Process of the behavior Filter
  • Custom implementationIActionFilterBehavior Filter
  • Use of exception Filters
Custom Implementation of IActionFilter behavior Filter

Let's continue with the example of the previous controller space. It doesn't matter if you haven't seen it, as long as you have a basic display page.

Figure 1

This is the initial page that calls the default Index method of IoCDemoController controller.

Next we will implement the custom IActionFilter type.

Code 1-1

1 public class MyCustomActionFilterAttribute: FilterAttribute, IActionFilter 2 {3 4 public void OnActionExecuted (ActionExecutedContext filterContext) 5 {6 filterContext. httpContext. response. write ("this is in the Controller Method:" + filterContext. actionDescriptor. actionName + "-after execution"); 7 8} 9 10 public void OnActionExecuting (ActionExecutingContext filterContext) 11 {12 filterContext. httpContext. response. write ("this is in the Controller Method:" + filterContext. actionDescriptor. actionName + "-before execution"); 13} 14}

In the previous article, we have already described the design of the type structure in the above Code, and we will not repeat it here. We have defined the behavior filter to use it.

After applying this filter, let's take a look at the running results:

Figure 2

We can try to put the filter on the Controller action to be executed, instead of the controller:

Code 1-2

 1     public class IoCDemoController : Controller 2     { 3         private IDataStandard _DataStandard; 4  5         public IoCDemoController(IDataStandard dataStandard) 6         { 7             _DataStandard = dataStandard; 8         } 9 10         [MyCustomActionFilter]11         public ActionResult Index()12         {13             return View(_DataStandard.GetProducts());14             15         }16     }

The result of running the command again is the same as that on the controller. The only difference is that the application scope of the filter is different. When you put the filter on the method, these filters are called only when you request the behavior of the controller, and the filter on the controller, any action in the request controller will be called.

Filter execution sequence

From the previous articles, we can see from the hard coding of the framework that the execution sequence of different types of controllers is authorization authentication filter, behavior filter, and result filter. There is also an exception filter, which is unrestricted and will trigger the call as long as there is an exception. Here we are talking about the calling sequence of the frameworks before different types of filters. How can we control the calling sequence between filters of the same type? Let's look at the example:

Code 1-3

1 public class MyCustomActionFileterOneAttribute: FilterAttribute, IActionFilter 2 {3 4 public void OnActionExecuted (ActionExecutedContext filterContext) 5 {6 filterContext. httpContext. response. write ("this is" + this. getType (). name + "filter in Controller Method:" + filterContext. actionDescriptor. actionName + "-output after execution"); 7} 8 9 public void OnActionExecuting (ActionExecutingContext filterContext) 10 {11 filterContext. httpContext. response. write ("this is" + this. getType (). name + "filter in Controller Method:" + filterContext. actionDescriptor. actionName + "-output before execution"); 12} 13}

In addition, the output example in the above 1-1 code is changed to the input Example 1-3, and then we add the newly defined behavior filter to the behavior method:

1         [MyCustomActionFilter]2         [MyCustomActionFileterOne]3         public ActionResult Index()4         {5             return View(_DataStandard.GetProducts());6         }

Let's take a look at the results.

Figure 3

When you see this result, some friends may say that it is executed in the order of the added position. Let's say that, let's modify it and add it to Index () the attribute variable Order in the two filters above the method comes to the IMvcFilter interface type. All filter types must be implemented, however, the FilterAttribute type in our custom settings has already been implemented for us.

Let's take a look at the modified sample code:

Code 1-4

1         [MyCustomActionFilter(Order=2)]2         [MyCustomActionFileterOne(Order=1)]3         public ActionResult Index()4         {5             return View(_DataStandard.GetProducts());6         }

Running result

There are many ways to use the filter. Here we will explain the IActionFilter Type Filter in this article.

 

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

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.