[MVC]Asp.net MVC生命周期


     public class MvcApplication : System.Web.HttpApplication
        {
            
public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute(
"{resource}.axd/{*pathInfo}");
               
                
//The controller route value is a special value that the System.Web.Mvc.MvcHandler class uses to call into the IControllerFactory interface.
                
//The basic route handler is an instance of IRouteHandler named MvcRouteHandler.
                
//We have complete control and could provide our own implementation of IRouteHandler if we wished.
                routes.MapRoute(
                    
"Default",                                              // Route name
                    "{controller}/{action}/{id}",                           // URL with parameters
                    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
                );
             
    
            }
    
            
protected void Application_Start()
            {
                RegisterRoutes(RouteTable.Routes);
            }
    

 

UrlRoutingMoudulePostResolveRequestCache阶段从RouteCollection中获取当前请求的RouteData.RouteData包含了一个请求处理对应的ControllerAction,RouteData这个作用贯穿请求的处理过程.RouteData中提取RouteHandler,这里默认是MvcRouteHandler,MvcRouteHandler获取HttpHandler,这里默认的是MvcHandler.

 


    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() }));
            }

            RequestData data2 
= new RequestData();
            data2.OriginalPath 
= context.Request.Path;
            data2.HttpHandler 
= httpHandler;
            context.Items[_requestDataKey] 
= data2;
            context.RewritePath(
"~/UrlRouting.axd");
        }

    }

}

rh 

  

MvcHandler.ProcessRequest()中首先使用HttpContextWrapperHttpContext进行封装,封装的目的是为了解耦以获得可测试性.然后从RequestContext.RouteData中提取Controller名称.
ControllerBuilder.GetControllerFactory –> ControllerFactory.CreateController –> IController.Execute

ControllerBase实现了IController接口,Initialize时将RequestContext封装成为ControllerContext,Controller继承自ControllerBase并实现抽象方法ExecuteCore()

 

mvchttp 

 

ExecuteCore,Controller首先从RouteData中获得ActionName,然后执行ActionInvoker.InvokeAction.

ActionInvoker中我们可以看到各种Filter,这是一种AOP实践:Action方法执行的前后执行若干方法.这里有四种Filter:ActionFilters,ResultFilters,AuthorizationFilters,ExceptionFilters.这四种Filter并不是封闭的,都有对应的接口,这四个只是默认实现.Filter的执行顺序是:AuthorizationFilter—>Action Filter.OnActionExecuting—>Action Method—>ActionFilter.OnActionExecuted.InvokeActionMethodWithFilters返回的结果是ActionExecutedContext,接下来将Controller执行OnResultExecuting 方法.ActionResult执行的结果可以是ViewResult,JsonResult,RedirectResult,ContentResult,或者是自定义的Result类型.

       如果返回的类型是ViewResult,我们先看一下ViewReuslt的继承关系:ViewResult–>ViewResultBase–>ActionResult,ViewResult包含两个属性ViewViewEngineCollection,实际上是包含了两个接口的实现:IViewEngine定义了怎么定位View/Partial View.IView定义了如何RenderView.默认的实现时WebFormViewWebFormViewEngine.

Filter OnResultExecuted 最后一步了,可以这里捕获异常.上面我们说过还有ExceptionFilters,如果前面过程中的异常没有被捕获那么最终都会到冒泡到ExceptionFilters.

  • RouteData中获得ActionName
  • ActionInvoker.InvokeAction
  • 通过ControllerContext获取ControllerDescriptor
  • FindAction-获取ActionDescriptor
  • GetFilters
  • ModelBinderRequest中的数据转换成Action方法需要的参数
  • AuthorizationFilter
  • Action Filter.OnActionExecuting
  • Action
  • ActionFilter.OnActionExecuted
  • ResultFilter.OnResultExecuting
  • ActionResult Execution
  • ResultFilter.OnResultExecuted
  • WebFormViewEngine.CreateView
  • WebFormView.Render
  • ResultFilter.OnExecuted

result 

 

控制权归还到HttpApplication完成后续的生命周期.

 

嗯哼,全文完.

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏