在asp.net Web表单中路由根地址

时间:2012-03-12 11:13:48

标签: asp.net routing webforms url-routing

我的web表单应用程序的global.asax中有以下RegisterRoutes方法

routes.MapPageRoute("about", "about", "~/about.aspx");
            routes.Add(
            "root",
            new Route("", new RootRouteHandler())
            );

第一条路线正常工作,即当我输入http://localhost:9898/about时,但当我转到根地址时,如'http:// localhost:9898',它不会调用RootRouteHandler。这是路由处理程序的代码

public class RootRouteHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var address = requestContext.HttpContext.Request.Headers["host"];
            string host = string.Empty;
            var index = address.IndexOf('.');
            if(index>0)
            {
                host = address.Substring(0, index);
            }
            var virtualPath = string.Format("~/{0}/default.aspx", host);
            var page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as Page;
            return page;
        }
    }

* 编辑: *当我添加

之类的路线时
routes.Add("root",new Route("default",new RootRouteHandler());

然后对地址http://localhost:9898/default调用处理程序,但我需要让它适用于根地址。任何想法?

1 个答案:

答案 0 :(得分:0)

我认为您需要创建一个新的RouteBase才能为主机路由并覆盖GetRouteData方法。考虑从RouteBase派生并为基于主机的路由提供功能,并将其添加到RouteCollection。

http://msdn.microsoft.com/en-us/library/system.web.routing.routebase.aspx