public static void RegisterRoutes(RouteCollection routes) { RouteConfigurationSection section = (RouteConfigurationSection)ConfigurationManager.GetSection("routeConfig"); string domainOnlyHost = ConstValue.WebDomainOnlyHost.Trim(); routes.Add("SellerSubDomainPage", new DomainRoute( "{subdomain}." + domainOnlyHost, "Store/{SellerSysNo}/{PageSysNo}", new { subdomain = "", controller = "Store", action = "Index", SellerSysNo = "{SellerSysNo}", PageSysNo = "{PageSysNo}", Preview = false } // Parameter defaults )); routes.Add("SellerDomainRoute", new DomainRoute( "{subdomain}." + domainOnlyHost, "", new { subdomain = "", controller = "Home", action = "Index" } )); routes.MapRoute(section); //<route name="Web_Index" url="" controller="Home" action="Index"></route> //RouteValueDictionary defaults = new RouteValueDictionary(); //defaults.Add("controller", "Home"); //defaults.Add("action", "Index"); //Route route = new Route("", new MvcRouteHandler()); //route.Defaults = defaults; //routes.Add("Web_Index", route); }
public static void RegisterRoutes(RouteCollection routes) { RouteConfigurationSection section = (RouteConfigurationSection)ConfigurationManager.GetSection("routeConfig"); routes.MapRoute(section); }
private static RouteConfigurationSection GetRouteConfig() { string cacheKey = "GetRouteConfigSection"; if (HttpRuntime.Cache[cacheKey] != null) { return((RouteConfigurationSection)HttpRuntime.Cache[cacheKey]); } RouteConfigurationSection section = (RouteConfigurationSection)ConfigurationManager.GetSection("routeConfig"); HttpRuntime.Cache.Insert(cacheKey, section, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration); return(section); }
public static void RegisterConfigurationBundles(this RouteCollection routes) { RouteConfigurationSection routesTableSection = GetRouteTableConfigurationSection(); if (routesTableSection == null || routesTableSection.Routes.Count <= 0) { return; } for (int routeIndex = 0; routeIndex < routesTableSection.Routes.Count; routeIndex++) { var routeElement = routesTableSection.Routes[routeIndex]; var route = new Route( routeElement.Url, GetDefaults(routeElement), GetConstraints(routeElement), GetDataTokens(routeElement), GetInstanceOfRouteHandler(routeElement)); routes.Add(routeElement.Name, route); } }
/// <summary> /// 根据RouteName自动构造包含多语言、是否需要SSL的URL /// </summary> /// <param name="routeName">路由名称,route.config中的路由名称</param> /// <param name="routeValues">路由参数,请与路由配置文件中的参数顺序保持一致</param> /// <returns>包含多语言,是否是https访问的绝对Url</returns> public static string BuildUrl(string routeName, params object[] routeValues) { RouteConfigurationSection section = GetRouteConfig(); //AreaItem curArea = null; RoutingItem curRouteItem = null; //先从Areas中查找 foreach (AreaItem area in section.Areas) { foreach (RoutingItem routingItem in area.Map) { if (routingItem.Name.ToLower().Trim() == routeName.Trim().ToLower()) { curRouteItem = routingItem; //curArea = area; break; } } if (curRouteItem != null) { break; } } //string lc = LanguageHelper.GetLanguageCode(); //如果不存在,则从Maps中查找 if (curRouteItem == null) { foreach (RoutingItem map in section.Map) { if (map.Name.ToLower().Trim() == routeName.Trim().ToLower()) { curRouteItem = map; break; } if (curRouteItem != null) { break; } } } if (curRouteItem == null) { //return string.Format("/{0}/{1}", lc, routeName); return(string.Format("/{0}", routeName)); } string relUrl = curRouteItem.Url.ToLower(); string hostName = GetHost(); string protocol = "http"; if (!string.IsNullOrWhiteSpace(curRouteItem.NeedSSL) && curRouteItem.NeedSSL.Trim() == "1" && Config.HaveSSLWebsite) { protocol = "https"; hostName = GetSecureHost(); } if (curRouteItem != null && routeValues != null && curRouteItem.Paramaters != null && routeValues.Length == curRouteItem.Paramaters.Count) { for (int i = 0; i < curRouteItem.Paramaters.Count; i++) { Parameter paramter = curRouteItem.Paramaters[i]; relUrl = relUrl.Replace(paramter.Value.ToLower(), routeValues[i].ToString()); } relUrl = relUrl.TrimStart("/".ToCharArray()); } //string urlLink = string.Format("{0}://{1}/{2}/{3}", protocol, hostName, lc, relUrl).ToLower(); string urlLink = string.Format("{0}://{1}/{2}", protocol, hostName, relUrl).ToLower(); return(urlLink); }
public ConfigRouteProvider() { RouteConfigurationSection routeConfigSection = ConfigurationManager.GetSection("routeConfig") as RouteConfigurationSection; if (routeConfigSection == null) { throw new ConfigException("The specified RouteConfigurationSection object does not exist or this is not an instance of RouteConfigurationSection."); } if (routeConfigSection.Routes != null && routeConfigSection.Routes.Count > 0) { string routeName; string routeUrl; string physicalFile; string[] urlParams; for (int routeIndex = 0; routeIndex < routeConfigSection.Routes.Count; routeIndex++) { RouteMappingElement routeMapping = routeConfigSection.Routes[routeIndex]; if (routeMapping == null) { continue; } routeName = routeMapping.Name; if (!routeName.HasValue()) { throw new ConfigException("The route name is null or empty. Please provide a route name."); } routeUrl = routeMapping.Url; Regex regex = new Regex("\\{" + @"([\w\d]+)" + "\\}"); MatchCollection matches = regex.Matches(routeUrl); if (matches != null && matches.Count > 0) { urlParams = new string[matches.Count]; for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++) { urlParams[matchIndex] = matches[matchIndex].Value.Trim(new char[] { '{', '}' }); } } if (!routeUrl.HasValue()) { throw new ConfigException("The route url is null or empty. Please provide a route url."); } physicalFile = routeMapping.PhysicalFile; if (!physicalFile.HasValue()) { throw new ConfigException("The physical path is null or empty. Please provide a physical path."); } var defaults = routeMapping.Defaults; var constraints = routeMapping.Constraints; RouteValueDictionary defaultDictionary = null; RouteValueDictionary constraintDictionary = null; if (defaults != null && defaults.Count > 0) { //{areacode}/{days} //{areacode, 010},{days, 2} defaultDictionary = new RouteValueDictionary(); for (int defaultIndex = 0; defaultIndex < defaults.Count; defaultIndex++) { string param = defaults[defaultIndex].Name; string value = defaults[defaultIndex].Value; defaultDictionary.Add(param, value); } } if (constraints != null && constraints.Count > 0) { //{areacode}/{days} //{ areacode, 0\d{2,3} }, { days, [1-3]{1} } constraintDictionary = new RouteValueDictionary(); for (int constraintIndex = 0; constraintIndex < constraints.Count; constraintIndex++) { string param = constraints[constraintIndex].Name; string value = constraints[constraintIndex].Value; constraintDictionary.Add(param, value); } } RouteItem route = new RouteItem(routeName, routeUrl, physicalFile) { Defaults = defaultDictionary, Constraints = constraintDictionary }; if (!this.routes.Contains(route)) { this.routes.Add(route); } } } }