ASP. net mvc 1.0 ControllerContext

Source: Internet
Author: User

ASP. net mvc transmits the environment parameters required for the execution process by encapsulating context objects.



Public class ControllerContext
{
Public virtual ControllerBase Controller {get; set ;}
Public virtual HttpContextBase HttpContext {get; set ;}
Public RequestContext {get; set ;}
Public virtual RouteData {get; set ;}
}

As the initial state and base class of the context, ControllerContext contains basic request information. AuthorizationContext, predictioncontext, and ViewContext have simple logic and do not need to spend more ink. What we really need to know is the impact of the Executing/Executed context on the execution process.

To make it easier to observe the results, we write test code to assist our analysis.

[AttributeUsage (AttributeTargets. Class | AttributeTargets. Method, AllowMultiple = true)]
Class MyActionFilterAttribute: ActionFilterAttribute
{
Public string Name {get; set ;}

Private void Print ()
{
Var method = new StackFrame (1). GetMethod ();
HttpContext. Current. Response. Write (String. Format ("{0}. {1} <br/>", this. Name, method. Name ));
}

Public override void OnActionExecuting (ActionExecutingContext filterContext)
{
Print ();
}

Public override void OnActionExecuted (ActionExecutedContext filterContext)
{
Print ();
}

Public override void OnResultExecuting (ResultExecutingContext filterContext)
{
Print ();
}

Public override void OnResultExecuted (ResultExecutedContext filterContext)
{
Print ();
}
}

Public class TestController: Controller
{
[MyActionFilter (Name = "Filter1", Order = 1)]
[MyActionFilter (Name = "Filter2", Order = 2)]
[MyActionFilter (Name = "Filter3", Order = 3)]
Public ActionResult Index ()
{
Response. Write ("Action Method! <Br/> ");
Return Content ("View Render! <Br/> ");
}
}

By the way, let's take a look at the execution results.

Filter1.OnActionExecuting
Filter2.OnActionExecuting
Filter3.OnActionExecuting

Action Method!

Filter3.OnActionExecuted
Filter2.OnActionExecuted
Filter1.OnActionExecuted

Filter1.OnResultExecuting
Filter2.OnResultExecuting
Filter3.OnResultExecuting

View Render!

Filter3.OnResultExecuted
Filter2.OnResultExecuted
Filter1.OnResultExecuted

1. ActionExecutingContext

In ActionExecutingContext, The Result attribute is the only one that may generate a ".

Public class ActionExecutingContext: ControllerContext
{
Public virtual ActionDescriptor {get; set ;}
Public virtual IDictionary <string, object> ActionParameters {get; set ;}
Public ActionResult Result {get; set ;}
}

Normally, the returned results of the Action Method are stored. However, check ControllerActionInvoker. InvokeActionMethodFilter ().

Internal static ActionExecutedContext InvokeActionMethodFilter (filter, preContext, continuation)
{
Filter. OnActionExecuting (preContext );
If (preContext. Result! = Null)
{
Return new ActionExecutedContext (preContext, preContext. ActionDescriptor,
True/* canceled */, null/* exception */)
{
Result = preContext. Result
};
}

...
}

If preContext. Result! = Null, the method is terminated. We know that this Method is nested and executed. That is to say, once preContext. Result is assigned a non-null value, subsequent filters will not be executed, including Action Method. Try it.

Class MyActionFilterAttribute: ActionFilterAttribute
{
...

Public override void OnActionExecuting (ActionExecutingContext filterContext)
{
Print ();
If (Name = "Filter2") filterContext. Result = new EmptyResult ();
}

...
}

Output:
Filter1.OnActionExecuting
Filter2.OnActionExecuting

Filter1.OnActionExecuted

Filter1.OnResultExecuting
Filter2.OnResultExecuting
Filter3.OnResultExecuting

Filter3.OnResultExecuted
Filter2.OnResultExecuted
Filter1.OnResultExecuted

  • Three pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • Next 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.