ASP.NET网站中的深度优先嵌套路由 [英] Depth-first nested routing in an ASP.NET website

查看:80
本文介绍了ASP.NET网站中的深度优先嵌套路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been exploring the System.Web.Routing namespace, playing with constraints and such, but I can't see a way to implement this.

我正在使用WebPages / Razor框架在ASP.NET网站( non-WAP,non-MVC )上工作。

I'm working on an ASP.NET website (non-WAP, non-MVC) using the WebPages/Razor framework.

I正在尝试实现嵌套路由的形式,其中路由可以包含仅在父级匹配时才尝试的子级路由;每个试图匹配URI剩余的孩子。

I'm trying to implement a form of "nested routing", where a route can contain child routes that are only attempted if the parent matches; each child attempting to match the "remainder" of the URI. A "depth-first" route match search, if you will.

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}"),
    }),
));

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

I've excluded necessary constraints/handlers (among other parameters) for brevity.

无论如何,穿过树的每一步都将匹配树的 {* remainder} 尾部。 URI。如果分支失败,它将向上移动到下一个,基本上是对以下内容进行测试:

In any case, each step descending through the tree would match the {*remainder} tail of the URI. If a branch fails, it moves up and on to the next, essentially testing something like:

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

现在,我当然不是在问请写编码解码器,而是向正确的方向做手势。

Now, I'm certainly not asking "please to write teh codez", but rather a gesture in the right direction.

我想在API的什么地方开始实现这样的功能?我可以找到无数关于编写路线,约束等的教程和信息,但是找不到有关扩展路由引擎的信息。

Where would I want to be looking in the API in order to begin implementing such a feature? I can find countless tutorials and information on writing routes, constraints, etc., but not on extending the routing engine.

附录

我将继续添加为凭单


  1. 请注意,我知道从这样的路由树生成URL会很复杂;

  1. Please note, I am aware that URL generation from a "routing tree" such as this would be complicated; it is not something I intend to implement.

我只是意识到一种迭代路由生成就足够了;所以我想我会尽快发布该答案。不,不是。边缘情况太多。

I just realized a sort of iterative route generation could suffice; so I guess I'll post that as a possible answer shortly. Nope, it wouldn't. Too many edge cases.


推荐答案

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

I have the following code but there is one point I am not sure how you want to handle it: do you know how many child can have a route at the maximum?

在Global.asax中:

In the Global.asax:

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

TestingRoutHandler类:

The TestingRoutHandler class:

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

这篇关于ASP.NET网站中的深度优先嵌套路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆