zoukankan      html  css  js  c++  java
  • 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返回给客户端
  • 相关阅读:
    ISCC 2018——write up
    图的存储结构(十字链表、邻接多重表、边集数组)
    图的存储结构
    树梅派(Raspberry Pi 3b)安装kali linux 2.0
    树梅派3B kali2.0 启用SSH进行远程登录
    VS+VAssistX自动添加注释
    libtiff库使用
    word采用尾注进行参考文献排版的一些问题
    vs2008安装opencv2.4.6
    Altera CYCLONE III FPGA BGA布线
  • 原文地址:https://www.cnblogs.com/answercard/p/2652538.html
Copyright © 2011-2022 走看看