void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 RegisterRoutes(); } // 页面存放目录 private readonly string[] _pageMapPath = { @"~/OrderManager/" }; /// <summary> /// 注册路由 /// </summary> private void RegisterRoutes() { var defaults = new RouteValueDictionary { { "name", "a" }, { "id", @"\d+" } }; RouteTable.Routes.Add("OrderList1", new Route("OrderList/{id}.html", defaults, new CustomRouteHandler("~/OrderList/OrderList.aspx"))); //var defaults = new RouteValueDictionary { { "name", "1" }, { "id", 2 } }; //RouteTable.Routes.Add("home1", // new Route("home_{Id}_{name}.html", defaults, new CustomRouteHandler("~/Index.aspx"))); RouteTable.Routes.Add("home", new Route("home", new CustomRouteHandler("~/Index.aspx"))); RouteTable.Routes.Add("home.html", new Route("home.html", new CustomRouteHandler("~/Index.aspx"))); RouteTable.Routes.Add("index", new Route("index", new CustomRouteHandler("~/Index.aspx"))); //将Index.html请求映射为Default.aspx RouteTable.Routes.Add("Index-1", new Route("Index.html", new CustomRouteHandler("~/Index.aspx"))); // 将About.html请求映射为About.aspx RouteTable.Routes.Add("login", new Route("login", new CustomRouteHandler("~/login.aspx"))); // 将About.html请求映射为About.aspx RouteTable.Routes.Add("login.html", new Route("login.html", new CustomRouteHandler("~/login.aspx"))); RouteTable.Routes.Add("login.shtml", new Route("login.shtml", new CustomRouteHandler("~/login.aspx"))); // 遍历页面存放目录,为每个.aspx页面添加路由映射 foreach (string mapPth in _pageMapPath) { string path = Server.MapPath(mapPth); var directoryInfo = new System.IO.DirectoryInfo(path); foreach (System.IO.FileInfo f in directoryInfo.GetFiles()) { string fileName = f.Name; if (fileName.EndsWith(".aspx")) { string routeName = fileName.Substring(0, fileName.Length - 5); string url = string.Concat(mapPth.Substring(2), routeName, ".html"); RouteTable.Routes.Add(routeName, new Route(url, new CustomRouteHandler(string.Concat(mapPth, fileName)))); } } } }