使用webforms在Url路由中隐藏参数的自定义路由

时间:2016-11-30 05:36:03

标签: c# asp.net webforms routing

MapPageRoute

的扩展方法
     public static Route MapPageRoute(this RouteCollection routes,          
          string routeName, string routeUrl, string physicalFile, bool    
         checkPhysicalUrlAccess, string routeValueNames
    , RouteValueDictionary defaults, RouteValueDictionary constraints)
                {

                    //   string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens
                    if (routeUrl == null)
                    {
                        throw new ArgumentNullException("routeUrl");
                    }
                    if (routeName == null)
                    {
                        throw new ArgumentNullException("routeName");
                    }
                    if(defaults == null)
                    {
                        defaults = new RouteValueDictionary { { "", "" } };
                    }
                    Route item = new RouteWithExclusions(routeUrl, new PageRouteHandler(physicalFile, checkPhysicalUrlAccess), routeValueNames)
                    {

                        Defaults = null,
                        Constraints = new RouteValueDictionary(constraints),
                        DataTokens = new RouteValueDictionary()
                    };

                    routes.Add(routeName, item);
                    return item;
                }

RouteWithExclusions 是从路线

继承的

这就是我宣布 CustomRouting

的方式
routes.MapPageRoute("BlogsByTitle",
                           "{BlogsTitle}/{Id}",
                            "~/ViewBlogs.aspx",
                            false,
                            "Id",
                            null,
                            new RouteValueDictionary { { "checkRoute", new      IsBlogConstraint() } });

在上面的路由中我隐藏了一些参数,所以在我的示例中我隐藏了Id

问题:

无法检索 VirtualPathData 。这是我的自定义VirtualPathData

  public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        if (requestContext == null)
        {
            throw new ArgumentNullException("requestContext");
        }

        // create new route data and include only non-excluded values
        RouteData excludedRouteData = new RouteData(this, this.RouteHandler);

        // add route values
        requestContext.RouteData.Values
            .Where(pair => !this.ExcludedRouteValuesNames.Contains(pair.Key, StringComparer.OrdinalIgnoreCase))
            .ToList()
            .ForEach(pair => excludedRouteData.Values.Add(pair.Key, pair.Value));
        // add data tokens
        requestContext.RouteData.DataTokens
            .ToList()
            .ForEach(pair => excludedRouteData.DataTokens.Add(pair.Key, pair.Value));

        // intermediary request context
        RequestContext currentContext = new RequestContext(new HttpContextWrapper(HttpContext.Current), excludedRouteData);

        // create new URL route values and include only none-excluded values
        RouteValueDictionary excludedRouteValues = new RouteValueDictionary(
            values
                .Where(v => !this.ExcludedRouteValuesNames.Contains(v.Key, StringComparer.OrdinalIgnoreCase))
                .ToDictionary(pair => pair.Key, pair => pair.Value)
        );

        VirtualPathData result = base.GetVirtualPath(currentContext, excludedRouteValues);
        return result;
    }

0 个答案:

没有答案