Depth-first nested routing in an ASP.NET website

旧街凉风 提交于 2019-12-05 15:07:01

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?

In the Global.asax:

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

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;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!