Deep Nested Routing on ASP.NET

I studied the System.Web.Routing , playing with restrictions, etc., but I see no way to implement this.

I am working on an ASP.NET website (not WAP, not MVC) using WebPages / Razor.

I am trying to implement a form of "nested routing" where the route may contain child routes that only try if the parent match; each child tries to match the "remainder" of the URI. Search the "depth-first" route if you want.

 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 have reduced the necessary constraints / handlers (among other parameters) for brevity.

In any case, each step descending through the tree will correspond to the tail of the {*remainder} URI. If the branch fails, it moves up and down 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, of course, I do not ask "please write the code", but rather a gesture in the right direction.

Where do I want to look for an API to start implementing such a function? I can find countless tutorials and information about writing routes, restrictions, etc., but not about extending the routing mechanism.


Adding
I will just add as orders

  • Note: I know that generating URLs from a routing tree such as this will be difficult; this is not what I intend to implement.

  • I just realized that iterative route generation could be enough; therefore, I think, I will publish this as a possible answer in the near future. No, it is not. Too many edge cases.

+4
source share
1 answer

I have the following code, but there is one point, I'm not sure how you want to deal with this: do you know how much a child can have a maximum route?

At Global.asax:

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

Class 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; } } } 
+1
source

All Articles