asp.net mvc的生命周期

asp.net mvc的生命周期主要分三个阶段
1、网址路由对比
2、找到对应的Controller执行相应的Action
3、执行View并返回结果

完整的生命周期
Request->UrlRoutingModule->RouteHandler->MvcHandler->
DefaultControllerFactory->Controller->View Factory->View->Response

执行control和view
当程序执行到MvcHandler,它的入口是ProcessRequest,在MvcHandler中的ProcessRequest

        protected internal virtual void ProcessRequest(HttpContextBase httpContext) {
            SecurityUtil.ProcessInApplicationTrust(() => {
                IController controller;
                IControllerFactory factory;
                ProcessRequestInit(httpContext, out controller, out factory);

                try {
                    controller.Execute(RequestContext);
                }
                finally {
                    factory.ReleaseController(controller);
                }
            });
        }
第一步:获取Controller,ControllerFactory

// 获取控制器的名称
string controllerName = RequestContext.RouteData.GetRequiredString("controller");

//获取控制器工厂,默认是DefaultControllerFactory
factory = ControllerBuilder.GetControllerFactory();

//创建控制器
controller = factory.CreateController(RequestContext, controllerName);


第二步:执行Controller的Action

 controller.Execute(RequestContext);
 最后调用Controller的ExecuteCore方法
        protected override void ExecuteCore() {
            // If code in this method needs to be updated, please also check the BeginExecuteCore() and
            // EndExecuteCore() methods of AsyncController to see if that code also must be updated.

            PossiblyLoadTempData();
            try {
                string actionName = RouteData.GetRequiredString("action");
                if (!ActionInvoker.InvokeAction(ControllerContext, actionName)) {
                    HandleUnknownAction(actionName);
                }
            }
            finally {
                PossiblySaveTempData();
            }
        }

第三步:执行View
如果从Action返回的是ViewResult,Mvc会调用实现IViewEngine对象的FindView方法,然后再调用实现IView(WebFormView)的Render()方法,将
Html返回给客户端

转载于:https://www.cnblogs.com/50614090/archive/2012/02/14/2350918.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值