asp.net mvc 流程初探

在知道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必须知道的:HttpModule,HttpHandler

 

 

下面简要描述下 asp.net mvc 处理流程:

 1、在Web. config中 注册

 

Code

2、在Global中设置 System.Web.Routing.RouteTable.Routes 的值,应用于解析Url,得到相应的Controller,Action,和RouteData

Code

 在MVC对RouteCollection 的扩展方法中,有MapRoute

Code

 

 

3、发送Http请求,因为在第1部注册了System.Web.Routing.UrlRoutingModule ,在执行到 HttpModule 时就调用IHttpModule接口的 Init 方法

Code

 

1)我们看 Init 中的调用 application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache);下的代码

Code

 

再看this.PostResolveRequestCache(context);的调用

Code

 

 

 我们发现 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); 下的代码

Code

 

Code

 

 我们通过 RequestData data = (RequestData) context.Items[_requestDataKey];得到了存入 RequestDataRequestData 对象,再通过 context.Handler = data.HttpHandler;设置处理Http请求的IhttpHandler对象,也就是MvcRouteHandler对象。

可以看出:1)我们得到了 MvcRouteHandler对象。2)我们把MvcRouteHandler对象赋给了context.Handler

4)进入出来http请求的 MvcRouteHandler对象

Code

 

通过 return new MvcHandler(requestContext); 我们发现,MvcRouteHandler 的作用是选择执行控制器操作的 IHttpHandler。

 

 5、我们看MvcHandler的处理代码

Code

 

6、controller.Execute(RequestContext);这一步是关键,它的作用是得到控制器,并执行。

我们发现, controller实现了IController接口,具体看看它的继承关系:

 

 原来 controller.Execute(RequestContext) 执行的是ControllerBase的Execute,我们进入看看:

Code

 

7、 在Execute方法中,调用了ExecuteCore,看看ExecuteCore的调用:

 

Code

 

可以看出 ExecuteCore ,执行的是 Controller 中的方法。

Code

 附近说明:【 ActionInvoker 的实际对象是实现 IActionInvoker 接口的对象,默认为 ControllerActionInvoker对象。

看看代码:

Code

 

 】

在ExecuteCore中,我们得到了action,并调用 ActionInvoker.InvokeAction(ControllerContext, actionName) 执行。

 8、看看 InvokeAction的代码:

 

Code

 

9. InvokeActionResult 其实就是反射调用 ActionResult 的  public abstract void ExecuteResult(ControllerContext context); 方法。

 

我们看看 ViewResultBase 对 ExecuteResult 的实现:

 

Code

 

 

1)进入 result = FindView(context); 其实 调用的是ViewResult的FindView的实现:

Code

 

2) 看看 FindView中的第一行代码 ViewEngineResult result = ViewEngineCollection.FindView(context, ViewName, MasterName);

在进入ViewEngineCollection.FindView看看:

 

Code

 

3)再看看看 ViewEngineCollection.FindView 中的Find调用:

 

Code

 

10) 最后看看 从控制器到输出的流程 (注:Model 随 ControllerContext 传递)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值