ASP.NET MVC 请求流程

时间:2023-03-08 21:31:50

一、应用程序启动

1.Application_Start方法,程序启动

ASP.NET MVC 请求流程

2.RegisterRoutes方法,注册路由

ASP.NET MVC 请求流程

3.System.Web.Mvc.RouteCollectionExtensions.MapRoute方法,出现了MvcRoutehandler对象

ASP.NET MVC 请求流程

二、请求进入

在 “$\Windows\Microsoft.NET\Framework\版本号\Config\Web.config“ 中可以找到 " <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />”

证明请求会经过System.Web.Routing.UrlRoutingModule.Init方法。

1.System.Web.Routing.UrlRoutingModule.Init方法

ASP.NET MVC 请求流程

2.System.Web.Routing.UrlRoutingModule.PostResolveRequestCache方法

ASP.NET MVC 请求流程

3.System.Web.Routing.RouteCollection.GetRouteData方法

ASP.NET MVC 请求流程

4.System.Web.Routing.Route.GetRouteDara方法

ASP.NET MVC 请求流程

这里的this.RouteHandler就是在第一部分中应用程序启动是实例化好的MvcRouteHandler对象。好了,我们现在已经得到了MvcRouteHandler实例,继续向下走,先返回第2步:

2.System.Web.Routing.UrlRoutingModule.PostResolveRequestCache方法

ASP.NET MVC 请求流程

3. System.Web.Mvc.MvcRouteHandler.GetHttpHandler方法,返回MvcHandler对象

ASP.NET MVC 请求流程

4.System.Web.Mvc.MvcHandler的构成方法

ASP.NET MVC 请求流程

发现这个类实现了IHttpHandler接口,那么不得不去看下ProcessRequest方法

5.System.Web.Mvc.MvcHandler.ProcessRequest方法

ASP.NET MVC 请求流程

6.System.Web.Mvc.MvcHandler.ProcessRequestInit方法

ASP.NET MVC 请求流程

返回到第5步

5.System.Web.Mvc.MvcHandler.ProcessRequest方法

ASP.NET MVC 请求流程

6.System.Web.Mvc.ControllerBase.Excete方法

ASP.NET MVC 请求流程

7.System.Web.Mvc.Controller.ExcuteCore方法

ASP.NET MVC 请求流程

8.System.Web.Mvc.Controller.ActionInvoker属性

ASP.NET MVC 请求流程

9.System.Web.Mvc.Controller.CreateActionInvoker方法

ASP.NET MVC 请求流程

返回了实现IActionInvoker接口的实例,返回第7步

7.System.Web.Mvc.Controller.ExcuteCore方法

ASP.NET MVC 请求流程

8.System.Web.Mvc.ControllerActionInvoker.InvokeAction方法

ASP.NET MVC 请求流程

9.System.Web.Mvc.ControllerActionInvoker.InvokeActionResult方法

ASP.NET MVC 请求流程

10.System.Web.Mvc.ActionResult类,封装一个操作方法的结果

ASP.NET MVC 请求流程

我们来看一下重写了ExcuteResult的ActionResult的派生类:

ASP.NET MVC 请求流程

我们就选ViewResultBase类中的ExcuteResult方法看看

11.System.Web.Mvc.ViewResultBase.ExcuteResult方法

ASP.NET MVC 请求流程

12.System.Web.Mvc.ViewResult.ExcuteResult方法

ASP.NET MVC 请求流程

13.System.Web.Mvc.ViewEngineCollection.FindView方法

ASP.NET MVC 请求流程

14.System.Web.Mvc.VirtualPathProviderViewEngine.FindView方法

ASP.NET MVC 请求流程

返回第13步

13.System.Web.Mvc.ViewEngineCollection.FindView方法

ASP.NET MVC 请求流程

14.System.Web.Mvc.ViewEngineCollection.Find方法

ASP.NET MVC 请求流程

15.System.Web.Mvc.ViewEngineCollection.Find方法

ASP.NET MVC 请求流程

至此,我们得到了ViewEngineResult对象,ViewEngineResult表示定位视图引擎的结果。

现在我们返回第11步,继续向下走

11.System.Web.Mvc.ViewResultBase.ExcuteResult方法

ASP.NET MVC 请求流程

12.System.Web.Mvc.BuildManagerCompiledView.Render方法

ASP.NET MVC 请求流程

13. System.Web.Mvc.RazorView.RenderView方法,在这里使用Razor视图引擎来展示页面

ASP.NET MVC 请求流程