In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
First, let's take a look at the life cycle of the classic Asp.net processing pipeline.
We know that an ASP.NET application can have multiple HttpModule, but only one HttpHandler, and the BeginProce***equest (or Proce***equest) of this HttpHandler is used to process and return the request. Looking at the declaration processing pipeline cycle, we can see that in this cycle of MapHttpHandler, the corresponding HttpHandler will be queried according to the requested HttpHandler, so how does it find it?
Look for the httpModules configuration section in the system web.config and find an IHttpModule configuration with a name of UrlRoutingModule-4.0 on the penultimate line, which is the key to finding HttpHandler. Let's take a look at the UrlRoutingModule code:
Protected virtual void Init (HttpApplication application) {if (application.Context.Items [_ contextKey]! = null) {return;} application.Context.Items [_ contextKey] = _ contextKey; application.PostResolveRequestCache + = OnApplicationPostResolveRequestCache;} private void OnApplicationPostResolveRequestCache (object sender, EventArgs e) {HttpApplication app = (HttpApplication) sender; HttpContextBase context = new HttpContextWrapper (app.Context); PostResolveRequestCache (context) } public virtual void PostResolveRequestCache (HttpContextBase context) {RouteData routeData = RouteCollection.GetRouteData (context); … IRouteHandler routeHandler = routeData.RouteHandler;. RequestContext requestContext = new RequestContext (context, routeData); context.Request.RequestContext = requestContext; IHttpHandler httpHandler = routeHandler.GetHttpHandler (requestContext); … Context.RemapHandler (httpHandler);}
You can see that UrlRoutingModule sets a PostResolveRequestCache event handling method, which finds the corresponding routing data RouteData (such as Controller name and Action name in Mvc) from RouteCollection through matching, then gets an instance of IRouteHandler from the property RouteHandler of RouteData, then gets the corresponding IHttpHandler instance from the IRouteHandler instance, and finally calls the RemapHandler method of HttpContext to re-set RemapHandlerInstance for HttpContext.
According to the previous asp.net initialization process analysis 2, we know that the classic mode and the integration mode use different IExecutionStep when obtaining Httphandler, and the classic mode uses MapHandlerExecutionStep integration mode using MaterializeHandlerExecutionStep. Check out the execution method Execute of both.
Let's take a look at MaterializeHandlerExecutionStep.
Void IExecutionStep.Execute () {HttpContext context = _ application.Context; HttpRequest request = context.Request; IHttpHandler handler = null; string configType = null;. If (context.RemapHandlerInstance! = null) {wr.SetScriptMapForRemapHandler (); context.Handler = context.RemapHandlerInstance;}. }
You can see that in MaterializeHandlerExecutionStep, if RemapHandlerInstance is set in HttpContext in the UrlRoutingModule module, then the Handler of HttpContext is set directly with RemapHandlerInstance.
And look at MapHandlerExecutionStep.
Void IExecutionStep.Execute () {HttpContext context = _ application.Context; HttpRequest request = context.Request;. Context.Handler = _ application.MapHttpHandler (context, request.RequestType, request.FilePathObject, request.PhysicalPathInternal, false / * useAppConfig*/); … }
In MapHandlerExecutionStep, the MapHttpHandler method of HttpApplication is called to set the Handler of HttpContext. Take a look at the MapHttpHandler code:
Internal IHttpHandler MapHttpHandler (HttpContext context, String requestType, VirtualPath path, String pathTranslated, bool useAppConfig) {IHttpHandler handler = (context.ServerExecuteDepth = = 0)? Context.RemapHandlerInstance: null; using (new ApplicationImpersonationContext ()) {if (handler! = null) {return handler;}. }
You can see from the first line of code that if the RemapHandlerInstance of HttpContext is not empty, it directly returns the RemapHandlerInstance of HttpContext (context.ServerExecuteDepth refers to whether the page uses HttpServerUtility.Execute to jump within the page). This also uses the RemapHandlerInstance set in HttpContext in the UrlRoutingModule module, but how to find HttpHandler based on the default extension match if HttpContext does not have a set RemapHandlerInstance is not discussed here.
From the above analysis, we can imagine that the route corresponds to the HttpHandler by registering the RouteData in the static RouteCollection property of the UrlRoutingModule and setting the IRouteHandler of the RouteData (an interface, only one method GetHttpHandler is used to get the HttpHandler). Let's take a look at how MvcHandler registers through routing. Let's first look at the implementation of RouteCollection.
Public RouteCollection RouteCollection {get {if (_ routeCollection = = null) {_ routeCollection = RouteTable.Routes;} return _ routeCollection;} set {routeCollection = value;}}
You can see that RouteCollection actually wraps the static Routes in RouteTable. If you have experience in Mvc project, it should look familiar. General Mvc programs generally have this section in Global.asax to register routes:
Protected void Application_Start () {. RouteConfig.RegisterRoutes (RouteTable.Routes);. } public class RouteConfig {public static void RegisterRoutes (RouteCollection routes) {routes.IgnoreRoute ("{resource} .axd / {* pathInfo}") Routes.MapRoute (name: "Default", url: "{controller} / {action} / {id}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional});}}
The secret of our defined routing setting MvcHandler is in the MapRoute method, which is an extension method, defined in System.Web.Mvc.RouteCollectionExtensions:
Public static Route MapRoute (this RouteCollection routes, string name, string url, object defaults, object constraints, string [] namespaces) {. Route route = new Route (url, new MvcRouteHandler ()) {Defaults = CreateRouteValueDictionaryUncached (defaults), Constraints = CreateRouteValueDictionaryUncached (constraints), DataTokens = new RouteValueDictionary ()};. Routes.Add (name, route); return route;}
You can see that the MapRoute registration route is bound with a MvcRouteHandler as an IRouteHandler. Let's see how MvcRouteHandler is implemented:
Protected virtual IHttpHandler GetHttpHandler (RequestContext requestContext) {requestContext.HttpContext.SetSessionStateBehavior (GetSessionStateBehavior (requestContext)); return new MvcHandler (requestContext);}
Here you finally see the code to create the MvcHandler.
At this point, we should have a clear understanding that we add all kinds of Route through the global static attribute set (RouteTable.Routes) (but before the HttpModule initialization cycle, we usually use the cycle created by HttpApplication to add the Route rules we need in the Application_Start method). Of course, we add the important HttpHandler of MvcHandler when adding routes. Then through UrlRoutingModule in the PostResolveRequestCache cycle, you can find the registered Route to get the requested RouteData and its attribute IRouteHandler instance (as for how the route is matched, we will continue to talk about it in the following chapter), and then through the IRouteHandler instance, you can get the IHttpHandler through GetHttpHandler and set it to the RemapHandlerInstance property of HttpContext. Finally, in the MapHttpHandler cycle, different HttpHandler is implemented by obtaining the RemapHandlerInstance of HttpContext to take over the URL that matches different routes.
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.