标签:
UrlRoutingModule
public virtual void PostResolveRequestCache(HttpContextBase context) { // Match the incoming URL against the route table RouteData routeData = RouteCollection.GetRouteData(context); // Do nothing if no route found if (routeData == null) { return; } // If a route was found, get an IHttpHandler from the route‘s RouteHandler IRouteHandler routeHandler = routeData.RouteHandler; if (routeHandler == null) { throw new InvalidOperationException( String.Format( CultureInfo.CurrentCulture, SR.GetString(SR.UrlRoutingModule_NoRouteHandler))); } // This is a special IRouteHandler that tells the routing module to stop processing // routes and to let the fallback handler handle the request. if (routeHandler is StopRoutingHandler) { return; } RequestContext requestContext = new RequestContext(context, routeData); // Dev10 766875 Adding RouteData to HttpContext context.Request.RequestContext = requestContext; IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);//创建MVCHandler 并将RequestContext存入
if (httpHandler == null) { throw new InvalidOperationException( String.Format( CultureInfo.CurrentUICulture, SR.GetString(SR.UrlRoutingModule_NoHttpHandler), routeHandler.GetType())); } if (httpHandler is UrlAuthFailureHandler) { if (FormsAuthenticationModule.FormsAuthRequired) { UrlAuthorizationModule.ReportUrlAuthorizationFailure(HttpContext.Current, this); return; } else { throw new HttpException(401, SR.GetString(SR.Assess_Denied_Description3)); } } // Remap IIS7 to our handler context.RemapHandler(httpHandler);//将MVCHandler存入HttpContext对象 }
RouteData routeData = RouteCollection.GetRouteData(context)
RouteCollection
public RouteData GetRouteData(HttpContextBase httpContext) { if (httpContext == null) { throw new ArgumentNullException("httpContext"); } if (httpContext.Request == null) { throw new ArgumentException(SR.GetString(SR.RouteTable_ContextMissingRequest), "httpContext"); } // Optimize performance when the route collection is empty. The main improvement is that we avoid taking // a read lock when the collection is empty. Without this check, the UrlRoutingModule causes a 25%-50% // regression in HelloWorld RPS due to lock contention. The UrlRoutingModule is now in the root web.config, // so we need to ensure the module is performant, especially when you are not using routing. // This check does introduce a slight if (Count == 0) { return null; } bool isRouteToExistingFile = false; bool doneRouteCheck = false; // We only want to do the route check once if (!RouteExistingFiles) { isRouteToExistingFile = IsRouteToExistingFile(httpContext); doneRouteCheck = true; if (isRouteToExistingFile) { // If we‘re not routing existing files and the file exists, we stop processing routes return null; } } // Go through all the configured routes and find the first one that returns a match using (GetReadLock()) { foreach (RouteBase route in this) { RouteData routeData = route.GetRouteData(httpContext); if (routeData != null) { // If we‘re not routing existing files on this route and the file exists, we also stop processing routes if (!route.RouteExistingFiles) { if (!doneRouteCheck) { isRouteToExistingFile = IsRouteToExistingFile(httpContext); doneRouteCheck = true; } if (isRouteToExistingFile) { return null; } } return routeData; } } } return null; }
RouteData routeData = route.GetRouteData(httpContext);
Route
public override RouteData GetRouteData(HttpContextBase httpContext) { // Parse incoming URL (we trim off the first two chars since they‘re always "~/") string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo; RouteValueDictionary values = _parsedRoute.Match(requestPath, Defaults); if (values == null) { // If we got back a null value set, that means the URL did not match return null; } RouteData routeData = new RouteData(this, RouteHandler); // Validate the values if (!ProcessConstraints(httpContext, values, RouteDirection.IncomingRequest)) { return null; } // Copy the matched values foreach (var value in values) { routeData.Values.Add(value.Key, value.Value); } // Copy the DataTokens from the Route to the RouteData if (DataTokens != null) { foreach (var prop in DataTokens) { routeData.DataTokens[prop.Key] = prop.Value; } } return routeData; }
Controller
protected override void ExecuteCore() { this.PossiblyLoadTempData(); try { string requiredString = this.RouteData.GetRequiredString("action"); if (this.ActionInvoker.InvokeAction(this.ControllerContext, requiredString)) return; this.HandleUnknownAction(requiredString); } finally { this.PossiblySaveTempData(); } }
ControllerActionInvoker
/// <summary> /// 使用指定的控制器上下文来调用指定操作。 /// </summary> /// /// <returns> /// 执行操作的结果。 /// </returns> /// <param name="controllerContext">控制器上下文。</param><param name="actionName">要调用的操作的名称。</param><exception cref="T:System.ArgumentNullException"><paramref name="controllerContext"/> 参数为 null。</exception><exception cref="T:System.ArgumentException"><paramref name="actionName"/> 参数为 null 或为空。</exception><exception cref="T:System.Threading.ThreadAbortException">线程在此操作的调用期间已中止。</exception><exception cref="T:System.Exception">在此操作的调用期间出现未指定的错误。</exception> public virtual bool InvokeAction(ControllerContext controllerContext, string actionName) { if (controllerContext == null) throw new ArgumentNullException("controllerContext"); if (string.IsNullOrEmpty(actionName)) throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName"); ControllerDescriptor controllerDescriptor = this.GetControllerDescriptor(controllerContext); ActionDescriptor action = this.FindAction(controllerContext, controllerDescriptor, actionName); if (action == null) return false; FilterInfo filters = this.GetFilters(controllerContext, action); try { AuthorizationContext authorizationContext = this.InvokeAuthorizationFilters(controllerContext, filters.AuthorizationFilters, action); if (authorizationContext.Result != null) { this.InvokeActionResult(controllerContext, authorizationContext.Result); } else { if (controllerContext.Controller.ValidateRequest) ControllerActionInvoker.ValidateRequest(controllerContext); IDictionary<string, object> parameterValues = this.GetParameterValues(controllerContext, action); ActionExecutedContext actionExecutedContext = this.InvokeActionMethodWithFilters(controllerContext, filters.ActionFilters, action, parameterValues); this.InvokeActionResultWithFilters(controllerContext, filters.ResultFilters, actionExecutedContext.Result); } } catch (ThreadAbortException ex) { throw; } catch (Exception ex) { ExceptionContext exceptionContext = this.InvokeExceptionFilters(controllerContext, filters.ExceptionFilters, ex); if (!exceptionContext.ExceptionHandled) throw; else this.InvokeActionResult(controllerContext, exceptionContext.Result); } return true; }
标签:
原文地址:http://www.cnblogs.com/aaa6818162/p/5006994.html