Beispiel #1
0
        public virtual void PostResolveRequestCache(HttpContextBase context)
        {
            //1.传入当前上下文对象,得到与当前请求匹配的SwiftRouteData对象
            SwiftRouteData routeData = this.SwiftRouteCollection.GetRouteData(context);

            if (routeData == null)
            {
                return;
            }
            //2.从SwiftRouteData对象里面得到当前的RouteHandler对象。
            IRouteHandler routeHandler = routeData.RouteHandler;

            if (routeHandler == null)
            {
                return;
            }

            //3.根据RequestContext对象得到处理当前请求的HttpHandler(MvcHandler)。
            IHttpHandler httpHandler = routeHandler.GetHttpHandler(routeData, context);

            if (httpHandler == null)
            {
                return;
            }

            //4.请求转到HttpHandler进行处理(进入到ProcessRequest方法)。这一步很重要,由这一步开始,请求才由UrlRoutingModule转到了MvcHandler里面
            context.RemapHandler(httpHandler);
        }
        //通过上下文对象得到当前请求的路由表
        public SwiftRouteData GetRouteData(HttpContextBase context)
        {
            var swiftRouteData = new SwiftRouteData();

            //1.配置RouteHandler实例,这里的RouteHandler是在全局配置里面写进来的
            swiftRouteData.RouteHandler = SwiftRoute.RouteHandler;

            //2.获取当前请求的虚拟路径和说明
            var virtualPath = context.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + context.Request.PathInfo;

            //3.先将默认路由配置写入当前请求的路由表
            //每次请求只能读取默认值,而不能覆盖默认值
            swiftRouteData.RouteValue = new Dictionary <string, object>();
            foreach (var key in this.SwiftRoute.DefaultPath)
            {
                swiftRouteData.RouteValue[key.Key] = key.Value;
            }

            //4.如果当前请求虚拟路径为空,则访问默认路由表。否则从当前请求的url里面去取当前的controller和action的名称
            if (!string.IsNullOrEmpty(virtualPath))
            {
                var arrTemplatePath = this.SwiftRoute.TemplateUrl.Split("{}/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                var arrRealPath     = virtualPath.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                for (var i = 0; i < arrTemplatePath.Length; i++)
                {
                    var realPath = arrRealPath.Length > i ? arrRealPath[i] : null;
                    if (realPath == null)
                    {
                        break;
                    }
                    swiftRouteData.RouteValue[arrTemplatePath[i]] = realPath;
                }
            }
            //5.去读当前请求的参数列表
            var querystring = context.Request.QueryString.ToString();

            if (string.IsNullOrEmpty(querystring))
            {
                return(swiftRouteData);
            }
            var parameters = querystring.Split("&".ToArray(), StringSplitOptions.RemoveEmptyEntries);
            var oparam     = new Dictionary <string, string>();

            foreach (var parameter in parameters)
            {
                var keyvalue = parameter.Split("=".ToArray());
                oparam[keyvalue[0]] = keyvalue[1];
            }
            swiftRouteData.RouteValue["parameters"] = oparam;
            return(swiftRouteData);
        }