我一直在探索
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}",));
为简洁起见,我已经排除了必要的约束/处理程序(以及其他参数).
在任何情况下,通过树下降的每个步骤都将匹配URI的{* remainder}尾部.如果分支失败,它会向上移动到下一个分支,基本上测试如下:
foo foo/bar foo/bar/baz foo/bar/zip foo/qux foo/qux/baz foo/qux/zip
现在,我当然不会问“请写代码”,而是一个正确方向的手势.
我想在哪里查看API才能开始实现这样的功能?我可以找到无数有关编写路由,约束等的教程和信息,但不能扩展路由引擎.
附录
我会继续添加作为权证
>请注意,我知道从这样的“路由树”生成URL会很复杂;这不是我打算实施的.
>我刚刚意识到一种迭代路由生成就足够了;所以我想我很快就会发布一个可能的答案.不,它不会.边缘情况太多.
解决方法
我有以下代码,但有一点我不确定你想如何处理它:你知道有多少孩子可以最大限度地拥有一条路线吗?
在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; } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。