在知道asp.net mvc 流程之前,必须知道完整的http请求在asp.net framework中的处理流程:
HttpRequest-->inetinfo.exe->ASPNET_ISAPI.DLL-->Http Pipeline-->ASPNET_WP.EXE-->HttpRuntime-->HttpApplication Factory-->HttpApplication-->HttpModule-->HttpHandler Factory-->HttpHandler-->HttpHandler.ProcessRequest()
具体参考过程
从底层了解ASP.NET体系结构下面简要描述下 asp.net mvc 处理流程:
1、在Web. config中 注册
- <modules>
-
- <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
- </modules>
复制代码2、在Global中设置 System.Web.Routing.RouteTable.Routes 的值,应用于解析Url,得到相应的Controller,Action,和RouteData
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- "Default", // Route name
- "{controller}/{action}/{id}", // URL with parameters
- new { controller = "Contact", action = "Index", id = "" } // Parameter defaults
- );
- }
- protected void Application_Start()
- {
- RegisterRoutes(RouteTable.Routes);
- }
复制代码在MVC对RouteCollection 的扩展方法中,有MapRoute
- public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces) {
- if (routes == null) {
- throw new ArgumentNullException("routes");
- }
- if (url == null) {
- throw new ArgumentNullException("url");
- }
- Route route = new Route(url, new MvcRouteHandler()) {
- Defaults = new RouteValueDictionary(defaults),
- Constraints = new RouteValueDictionary(constraints)
- };
- if ((namespaces != null) && (namespaces.Length > 0)) {
- route.DataTokens = new RouteValueDictionary();
- route.DataTokens["Namespaces"] = namespaces;
- }
- routes.Add(name, route);
- return route;
- }
复制代码3、发送Http请求,因为在第1部注册了System.Web.Routing.UrlRoutingModule ,在执行到 HttpModule 时就调用IHttpModule接口的 Init 方法
- protected virtual void Init(HttpApplication application)
- {
- application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache);
- application.PostMapRequestHandler += new EventHandler(this.OnApplicationPostMapRequestHandler);
- }
复制代码1)我们看 Init 中的调用 application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache);下的代码
- private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
- {
- HttpContextBase context = new HttpContextWrapper(((HttpApplication) sender).Context);
- this.PostResolveRequestCache(context);
- }
复制代码再看this.PostResolveRequestCache(context);的调用
- public virtual void PostResolveRequestCache(HttpContextBase context)
- {
- RouteData routeData = this.RouteCollection.GetRouteData(context);
- if (routeData != null)
- {
- IRouteHandler routeHandler = routeData.RouteHandler;
- if (routeHandler == null)
- {
- throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoRouteHandler, new object[0]));
- }
- if (!(routeHandler is StopRoutingHandler))
- {
- RequestContext requestContext = new RequestContext(context, routeData);
- IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
- if (httpHandler == null)
- {
- throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoHttpHandler, new object[] { routeHandler.GetType() }));
- }
- context.Items[_requestDataKey] = new RequestData { OriginalPath = context.Request.Path, HttpHandler = httpHandler };
- context.RewritePath("~/UrlRouting.axd");
- }
- }
- }
复制代码我们发现 IRouteHandler routeHandler = routeData.RouteHandler; 行把routeHandler赋了具体值。但 routeData.RouteHandler如何来的? 在第2部MapRoute的调用中,我们得到了它。通过routeHandler,我们得到了 IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);然后,通过context.Items[_requestDataKey] = new RequestData { OriginalPath = context.Request.Path, HttpHandler = httpHandler };我们把httpHandler 存入context.Items字段中。
2) 我们看 Init 中的调用 application.PostMapRequestHandler += new EventHandler(this.OnApplicationPostMapRequestHandler); 下的代码
- private void OnApplicationPostMapRequestHandler(object sender, EventArgs e)
- {
- HttpContextBase context = new HttpContextWrapper(((HttpApplication) sender).Context);
- this.PostMapRequestHandler(context);
- }
复制代码- public virtual void PostMapRequestHandler(HttpContextBase context)
- {
- RequestData data = (RequestData) context.Items[_requestDataKey];
- if (data != null)
- {
- context.RewritePath(data.OriginalPath);
- context.Handler = data.HttpHandler;
- }
- }
复制代码我们通过 RequestData data = (RequestData) context.Items[_requestDataKey];得到了存入 RequestData的RequestData 对象,再通过 context.Handler = data.HttpHandler;设置处理Http请求的IhttpHandler对象,也就是MvcRouteHandler对象。
可以看出:1)我们得到了 MvcRouteHandler对象。2)我们把MvcRouteHandler对象赋给了context.Handler