• MVC系统学习1—MVC执行流程


     用MVC来做开发也有一段时间了,但是感觉一直没入门,就徘徊在似懂非懂的层次,和去年刚毕业学习WebForm时一样,当时通过张子阳老兄的几篇文章,明白了请求处理流程,页面生命周期才真正明白了WebForm的强大。由于MVC的学习资料比较少,牛人的技术博客也只是讲一些基础的而已。因此决定通过Asp.Net MVC源码来学习,由于是开源的,也不用Reflector作为辅助工具。首先还是明白下MVC的请求处理流程。有参考了MSDN上面的文章(http://msdn.microsoft.com/zh-cn/library/dd381612.aspx)

          当应用程序第一次接受请求的时候,在Global.asax文件中,Route对象会添加到RouteTable对象中。RegisterRoutes函数就是我们实现的路由注册函数。

    protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                RegisterRoutes(RouteTable.Routes);
            }<br>  public static void RegisterRoutes(RouteCollection routes)<br>        {<br>            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");<br><br>            routes.MapRoute(<br>                "Default",<br>                "{controller}/{action}/{id}", <br>                new { controller = "Home", action = "Index", id = UrlParameter.Optional } <br>            );<br>        }<br>

          通过MVC源码在了解上面这一步的大致实现过程 。在RegisterRoutes函数中,通过调用MapRoute(扩展方法)来实现向RouteCollection添加Route对象。最后调用的是下面的方法。

    View Code

          可以发现上面的方法实现创建一个Route对像,并将其添加到RouteCollection,然后将其返回。接下来了解下Route类。Route继承自RouteBase类.其有几个比较重要的属性。

           上面这前三个成员的类型是RouteValueDictionary,而上面的函数中,是调用下面的这个函数来实现向Route的Defaults等添加数据。如注释,就是将controller,action,id添加到Defaults的键中,而将Home,Index加入到相应的键对应的值。而对于RouteHandler的作用等下再讲。这样就完成了一个Route对象的创建并且添加到RouteCollection中,而这个RouteCollection也就是RouteTable的成员Routes.

    private void AddValues(object values)
           {
               if (values != null)
               {
                   //这里分解出 new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                   //然后填充到字典里面
                   foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
                   {
                       object obj2 = descriptor.GetValue(values);
                       this.Add(descriptor.Name, obj2);
                   }
               }
           }

          在Asp.Net管道中执行MVC请求的HttpModule是UrlRoutingModule模块。UrlRoutingModule使用RouteTable 集合中第一个匹配的 Route 对象来创建 RouteData 对象,然后使用所创建的对象创建 RequestContext 对象。UrlRoutingModule里面有一个RouteCollection属性,其就是通过RouteTable.Routes来实现赋值的。

    public System.Web.Routing.RouteCollection RouteCollection
            {
                get
                {
                    //TODO:使用RouteTable的Routes成员来初始化routeCollection
                    if (this._routeCollection == null)
                    {
                        this._routeCollection = RouteTable.Routes;
                    }
                    return this._routeCollection;
                }
                set
                {
                    this._routeCollection = value;
                }
            }

           在UrlRoutingModule中,注册了两个HttpApplication公开的事件。其中是在OnApplicationPostResolveRequestCache方法里面实现路由匹配,因为其里面又调用了一个this.PostResolveRequestCache(context)方法,而在这个方法里面的第一句就是 RouteData routeData = this.RouteCollection.GetRouteData(context)。根据请求上下文来获取RouteData。路由匹配的奥秘貌似就在这里。

    protected virtual void Init(HttpApplication application)
           {
               application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache);
               application.PostMapRequestHandler += new EventHandler(this.OnApplicationPostMapRequestHandler);
           }

            this.RouteCollection.GetRouteData(context)方法里面最后又这么几句代码。通过遍历当前的RouteCollection来实现RouteData的获取,如果获取到则立即返回。这里其实遍历的单个对象不是RouteBase而是Route,而Route继承自RouteBase类,并且重写了RouteBase的抽象函数GetRouteData(为什么这么做,待研究?)。而路由匹配的实现也是在Route类里重写的GetRouteData方法。

    foreach (RouteBase base2 in this)
                   {
                       RouteData routeData = base2.GetRouteData(httpContext);
                       if (routeData != null)
                       {
                           return routeData;
                       }
                   }

         Route的GetRouteData方法

    public override RouteData GetRouteData(HttpContextBase httpContext)
            {
                //获取请求的虚拟路径
                 string virtualPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;
               //TODO:接下来进行路径的匹配
                 RouteValueDictionary values = this._parsedRoute.Match(virtualPath, this.Defaults);
                if (values == null)
                {
                    return null;
                }
                RouteData data = new RouteData(this, this.RouteHandler);
                //如果不通过约束则返回空
                 if (!this.ProcessConstraints(httpContext, values, RouteDirection.IncomingRequest))
                {
                    return null;
                }
                foreach (KeyValuePair<string, object> pair in values)
                {
                    data.Values.Add(pair.Key, pair.Value);
                }
                if (this.DataTokens != null)
                {
                    foreach (KeyValuePair<string, object> pair2 in this.DataTokens)
                    {
                        data.DataTokens[pair2.Key] = pair2.Value;
                    }
                }
                return data;
            }

         最后是在这个方法下面调用ParsedRoute的Match进行路由匹配

    明确个目标,一直走下去
  • 相关阅读:
    sublime text 配置本地静态服务器方法
    js中获得当前时间是年份和月份
    如何在Intellij IDEA中拉svn分支?
    快速上手seajs模块化以及案例
    webpack 配置多页面应用的一次尝试
    【Gitlab】gitlab-CI 持续集成以及runner的配置简版
    【vue】elementUI报错:_self.$scopedSlots.default is not a function
    【webpack】webpack多版本控制方案
    vuepress博客主题—vuepress-theme-reco
    reco-fetch
  • 原文地址:https://www.cnblogs.com/fhlj/p/3615216.html
Copyright © 2020-2023  润新知