ASP.NET 请求处理 二(MVC流程)
//MVC 处理流程
public class UrlRoutingModule : IHttpModule{ //mvc框架注册了一个IHttpModule 在里面注册了PostResolveRequestCache事件来处理请求路由 protected virtual void Init(HttpApplication application) { if (application.Context.Items[UrlRoutingModule._contextKey] != null) { return; } application.Context.Items[UrlRoutingModule._contextKey] = UrlRoutingModule._contextKey; application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache); } private void OnApplicationPostResolveRequestCache(object sender, EventArgs e) { HttpContextBase context = new HttpContextWrapper(((HttpApplication)sender).Context); this.PostResolveRequestCache(context); } public virtual void PostResolveRequestCache(HttpContextBase context) { //从控制器路由表里面找到合适的页面路由 RouteData routeData = this.RouteCollection.GetRouteData(context); if (routeData == null) { return; } //从RouteData里找到RouteHandler MVC默认是MvcRouteHandler来处理的 IRouteHandler routeHandler = routeData.RouteHandler; if (routeHandler == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.GetString("UrlRoutingModule_NoRouteHandler"), new object[0])); } if (routeHandler is StopRoutingHandler) { return; } RequestContext requestContext = new RequestContext(context, routeData); context.Request.RequestContext = requestContext; //调用MvcRouteHandler.GetHttpHandler 获取 MvcHandler 对象 IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext); if (httpHandler == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, SR.GetString("UrlRoutingModule_NoHttpHandler"), new object[] { routeHandler.GetType() })); } if (!(httpHandler is UrlAuthFailureHandler)) { //将Handler保存为remapHandler MvcHandler 接管http处理流程 context.RemapHandler(httpHandler); return; } if (FormsAuthenticationModule.FormsAuthRequired) { UrlAuthorizationModule.ReportUrlAuthorizationFailure(HttpContext.Current, this); return; } throw new HttpException(401, SR.GetString("Assess_Denied_Description3")); } } //RouteCollectionRoute.GetRouteData 遍历路由列表 public override RouteData GetRouteData(HttpContextBase httpContext) { List<RouteData> list = new List<RouteData>(); foreach (RouteBase current in this._subRoutes) { RouteData routeData = current.GetRouteData(httpContext); if (routeData != null) { list.Add(routeData); } } return RouteCollectionRoute.CreateDirectRouteMatch(this, list); } //Route.GetRouteData 分析路由数据是否满足url的解析条件 public override RouteData GetRouteData(HttpContextBase httpContext) { string virtualPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo; RouteValueDictionary routeValueDictionary = this._parsedRoute.Match(virtualPath, this.Defaults); if (routeValueDictionary == null) { return null; } RouteData routeData = new RouteData(this, this.RouteHandler); if (!this.ProcessConstraints(httpContext, routeValueDictionary, RouteDirection.IncomingRequest)) { return null; } foreach (KeyValuePair<string, object> current in routeValueDictionary) { routeData.Values.Add(current.Key, current.Value); } if (this.DataTokens != null) { foreach (KeyValuePair<string, object> current2 in this.DataTokens) { routeData.DataTokens[current2.Key] = current2.Value; } } return routeData; } //MvcHandler.ProcessRequest protected internal virtual void ProcessRequest(HttpContextBase httpContext) { IController controller; IControllerFactory controllerFactory; //从controllerFactory中取出或新建Controller this.ProcessRequestInit(httpContext, out controller, out controllerFactory); try { //执行controller controller.Execute(this.RequestContext); } finally { //释放controller controllerFactory.ReleaseController(controller); } }