How is the actionfilter in asp.net mvc executed?

Source: Internet
Author: User

In the four filters (filter) in asp.net mvc, Actionfilter is directly applied to an action method that intercepts the call before and after the target action method performs some extra action. This is a typical AOP design that can be implemented by defining actionfilter if we need to perform some action before and after executing an action method. This article focuses on one more actionfilter implementation mechanism applied to the same action method.

First, Actionfilter

Actionfilter allows us to intercept calls before and after the target action method to perform additional operations, and all actionfilter implement an interface iactionfilter with the following definitions.

1:public Interface Iactionfilter
2: {
3:void onactionexecuting (ActionExecutingContext filtercontext);
4:void onactionexecuted (ActionExecutedContext filtercontext);
5:}
6:
7:public class Actionexecutingcontext:controllercontext
8: {
9:public ActionExecutingContext ();
10:public ActionExecutingContext (ControllerContext controllercontext, Actiondescriptor ActionDescriptor, Idictionary<string, object> actionparameters);
11:
12:public virtual Actiondescriptor actiondescriptor {get; set;}
13:public virtual idictionary<string, object> actionparameters {get; set;}
14:public ActionResult result {get; set;}
15:}
16:
17:public class Actionexecutedcontext:controllercontext
18: {
19:public ActionExecutedContext ();
20:public ActionExecutedContext (ControllerContext controllercontext, Actiondescriptor ActionDescriptor, BOOL Cancele D, Exception Exception);
21st:
22:public virtual Actiondescriptor actiondescriptor {get; set;}
23:public virtual bool Canceled {get; set;}
24:public virtual Exception Exception {get; set;}
25:public bool exceptionhandled {get; set;}
26:public ActionResult result {get; set;}
27:}

As shown in the code snippet above, two methods onactionexecuting and onactionexecuted are defined in the Iactionfilter interface, which are called before and after the target action method is executed. Their parameter types are ActionExecutingContext and ActionExecutedContext respectively. Both of these contexts have types that are controllercontext subclasses.

We can get the Actiondescriptor from the ActionExecutingContext object to describe the current action, and the argument list. Actionfilter can assign values to the result property of a ActionExecutingContext object in the OnActionExecuting method to directly respond to the current request. Once the ActionExecutingContext result property is successfully assigned, the execution of the subsequent Actionfilter and the final target method is terminated.

ActionExecutedContext has an additional three properties, exception represents the exception thrown during the execution of the action method, and Exceptionhandled is a token that indicates whether the exception has been processed. The Canceled property indicates that the execution of the entire Actionfilter chain and the target action method has not been completed and is terminated halfway through.

II. implementation mechanisms of the Actionfilter

When Actioninvoker executes the target action method, the filter object used to encapsulate Actionfilter is sorted according to the order and scope properties. Then create a ActionExecutingContext object based on the current ControllerContext and Actiondescriptro. And then calls all Actionfilter's onactionexecuting methods as parameters in turn.

After that, the real target action method is executed, and Actioninvoker subsequently performs subsequent filtering operations. Specifically, it creates a ActionExecutedContext object based on the exception thrown during the current ControllerContext, Actiondescriptro, and action method execution. The ActionExecutedContext Cancel property is False, and if the action method returns a ActionResult object, the object will act as the result property of the ActionExecutedContext.

Next, in reverse order, the onactionexecuted method of the Actionfilter object is called, and Actionfilter can modify the ActionExecutedContext result property during execution. After the execution of the entire Actionfilter chain completes, the ActionResult returned by the ActionExecutedContext result property will be used as a response to the current request. The right diagram basically reflects the execution of the entire Actionfilter chain, along with the target action.

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.