In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "sample Analysis of Controller Activation and URL routing", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of Controller Activation and URL routing".
Controller activation and URL routing ASP.NET routing system is the first barrier for HTTP requests to arrive at the server. It matches intercepted requests and parses routing information including target Controller and Action names according to registered routing rules. Now that ControllerBuilder has a ControllerFactory for activating Controller objects, let's see how the two combine.
Through the introduction of Chapter 2, "URL routing", we know that the core of the ASP.NET routing system is a HttpModule called UrlRoutingModule, which is implemented by registering the dynamic mapping of PostResolveRequestCache events representing HttpApplication to HttpHandler. Specifically, it matches the request and obtains a RouteData object through the global routing table represented by the static attribute Routes of RouteTable. RouteData has a property RouteHandler that implements the interface IRouteHandler, and the HttpHandler object that is eventually mapped to the current request can be obtained through the GetHttpHandler method of this property.
For ASP.NET MVC applications, the RouteHandler attribute type of RouteData is MvcRouteHandler, and the HttpHandler provision mechanism implemented in MvcRouteHandler can be basically (not exactly equivalent) through the following code. MvcRouteHandler maintains a ControllerFactory object that can be specified in the constructor or obtained directly by calling the GetControllerFactory method of the current ControllerBuilder if the assignment is not displayed.
Public class MvcRouteHandler: IRouteHandler
{
Private IControllerFactory _ controllerFactory
Public MvcRouteHandler (): this (ControllerBuilder.Current
.GetControllerFactory ()
{}
Public MvcRouteHandler (IControllerFactory controllerFactory)
{
_ controllerFactory = controllerFactory
}
IHttpHandler IRouteHandler.GetHttpHandler (RequestContext requestContext)
{
String controllerName = (string) requestContext.RouteData
.GetRequiredString ("controller")
SessionStateBehavior sessionStateBehavior = _ controllerFactory
.GetControllerSessionrequestContext, controllerName)
RequestContext.HttpContext.SetSessionStatesessionStateBehavior)
< /p>Return new MvcHandler (requestContext)
}
}
In the GetHttpHandler method used to provide HttpHandler, in addition to returning a MvcHandler object that implements the IHttpHandler interface, you also need to set the session-state behavior mode of the current HTTP context. The specific implementation is to get the name of Controller through the RouteData object contained in RequestContext, which is passed into the GetControllerSessionBehavior method of ControllerFactory together with the RequestContext object to get an enumeration of type SessionStateBehavior. Finally, the current HTTP context (actually a HttpContextWrapper object) is obtained through RequestContext, and its SetSessionStateBehavior method is called to set the session-state behavior.
Through the introduction of Chapter 2, "URL routing", we know that the RouteHandler attribute in RouteData originally comes from the corresponding routing object, and when we call the extension method MapRoute method of RouteCollection, the RouteHandler corresponding to the registered Route object is a MvcRouteHandler object. Because the ControllerFactory is not explicitly specified when the MvcRouteHandler object is created, the ControllerFactory obtained through the GetControllerFactory method of the current ControllerBuilder is used by default.
The ControllerFactory obtained through the GetControllerFactory method of the current ControllerBuilder is only used to obtain the session-state behavior pattern, while MvcHandler actually uses it to create the Controller. The following code snippet basically embodies the definition of MvcHandler, whose logic for request processing is defined in the BeginProce***equest method.
Public class MvcHandler: IHttpAsyncHandler, IHttpHandler, IRequiresSessionState
{
/ / other members
Public RequestContext RequestContext {get; private set;}
Public bool IsReusable
{
Get {return false;}
}
Public MvcHandler (RequestContext requestContext)
{
This.RequestContext = requestContext
}
IAsyncResult BeginProce***equest (HttpContext context, AsyncCallback cb
Object extraData)
{
IControllerFactory controllerFactory =
ControllerBuilder.Current.GetControllerFactory ()
String controllerName =
This.RequestContext.RouteData.GetRequiredString ("controller")
IController controller = controllerFactory
.CreateController (this.RequestContext, controllerName)
If (controller is IAsyncController)
{
Try
{
/ / call the BeginExecute/EndExecute method to execute Controller asynchronously
}
Finally
{
ControllerFactory.ReleaseController (controller)
}
}
Else
{
Try
{
/ / call the Execute method to execute Controller asynchronously
}
Finally
{
ControllerFactory.ReleaseController (controller)
}
}
}
}
Because MvcHandler implements both the IHttpHandler and IHttpAsyncHandler interfaces, it is always executed asynchronously (calling the BeginProce***equest/EndProce***equest method). The BeginProce***equest method gets the name of the target Controller through the RequestContext object, and then activates the Controller object using the ControllerFactory created by the current ControllerBuilder. If the Controller type implements the IAsyncController interface, the Controller is executed asynchronously, otherwise synchronously. After the activated Controller object is executed, MvcHandler calls ControllerFactory's ReleaseController to clean it up.
The above is all the contents of the article "sample Analysis of Controller Activation and URL routing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.