4

我一直在探索System.Web.Routing命名空间,使用约束等,但我看不到实现这一点的方法。

我正在使用 WebPages/Razor 框架开发 ASP.NET 网站(非 WAP、非 MVC )。

我正在尝试实现一种“嵌套路由”形式,其中路由可以包含仅在父匹配时才尝试的子路由;每个孩子都试图匹配 URI 的“剩余部分”。一个“深度优先”的路线匹配搜索,如果你愿意的话。

routes.Add(new ParentRoute("{foo}/{*remainder}", new[] {
    new ParentRoute("{bar}/{*remainder}", new[] {
        new Route("{baz}"),
        new Route("{zip}"),
    }),
    new ParentRoute("{qux}/{*remainder}", new[] {
        new Route("{baz}"),
        new Route("{zip}"),
    }),
));

为简洁起见,我排除了必要的约束/处理程序(以及其他参数)。

在任何情况下,通过树向下的每一步都将匹配{*remainder}URI 的尾部。如果一个分支失败,它会向上移动到下一个,本质上是测试类似的东西:

foo
  foo/bar
    foo/bar/baz
    foo/bar/zip
  foo/qux
    foo/qux/baz
    foo/qux/zip

现在,我当然不是在问“请写代码”,而是在正确方向上的一种姿态。

为了开始实现这样的功能,我想在哪里查看 API?我可以找到无数关于编写路由、约束等的教程和信息,但在扩展路由引擎方面却找不到。


附录
我将继续添加为认股权证

  1. 请注意,我知道从这样的“路由树”生成 URL 会很复杂;这不是我打算实施的事情。

  2. 我刚刚意识到一种迭代路线生成就足够了;所以我想我会尽快将其作为可能的答案发布。不,它不会。边缘情况太多。

4

1 回答 1

1

我有以下代码,但有一点我不确定你想如何处理它:你知道最多有多少孩子可以拥有一条路线吗?

在 Global.asax 中:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new Route("test/{path}", new RouteValueDictionary { { "path", string.Empty } }, new TestingRouteHandler()));
    }

TestingRoutHandler 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web;
using System.Web.UI;
using System.Web.Compilation;

namespace WebApplication1
{
    class TestingRouteHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            //This is where you should treat the request, test if the file exists and if not, use the parent part of the url
            string aspxFileName = string.Format("~/{0}.aspx", requestContext.HttpContext.Request.Url.PathAndQuery.Replace("/", string.Empty));

            return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(aspxFileName, typeof(Page)) as Page;
        }
    }
}
于 2012-12-20T20:57:27.920 回答