Generally, we write the Route rules directly in the Global. asax. cs file, for example:
public static void RegisterRoutes(RouteCollection routes){ routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults );}
In this way, after the program is compiled and deployed, we have to modify the Global rule in the program. asax. in the cs file, the Route rule => compilation => deployment does not support dynamic configuration.
In fact, we can achieve dynamic configuration. We can write this Route rule into an XML file, for example:
<RouteConfiguration> <ignore> <! -- Ignore the Route of the. axd file and process it directly --> <add url = "{resource}. axd/{* pathInfo}"> <constraints> <! -- Add constraints. value is gibberish, only for demonstration --> <add name = "resource" value = "\ w"/> </constraints> </add> </ignore> <map> <route name =" post "url =" Post/{id} "controller =" Home "action =" Post "> <parameters> <! -- Add parameter default value --> <! -- Add constraints, the id must be GUID --> <add name = "id" value = "" constraint = "[\ da-f] {8}-[\ da-f] {4 }- [\ da-f] {4}-[\ da-f] {4}-[\ da-f] {12} "/> </parameters> </route> <route name = "Default" url = "{controller}/{action}/{id}" controller = "Home" action = "Index"> <parameters> <add name = "id" value = ""/> </parameters> </route> </map> </RouteConfiguration>
For example, we use configSection in Web. config to write our custom configuration section to configure the above XML file. First, I need to write a defined configSection that inherits from the ConfigurationSection, as shown below:
public class MvcRouteConfigurationSection : ConfigurationSection{ [ConfigurationProperty("ignore", IsRequired=false)] public IgnoreCollection Ignore { get { return (IgnoreCollection)(this["ignore"]); } } [ConfigurationProperty("map", IsRequired=false)] public RoutingCollection Map { get { return (RoutingCollection)(this["map"]); } }}
Then we write an extension method to customize and write the configured Route rule to the program routing table:
public static void MapRoute( this System.Web.Routing.RouteCollection routes, MvcRouteConfigurationSection section ){ // Manipulate the Ignore List foreach(IgnoreItem ignoreItem in section.Ignore) { RouteValueDictionary ignoreConstraints = new RouteValueDictionary(); foreach (Constraint constraint in ignoreItem.Constraints) ignoreConstraints.Add(constraint.Name, constraint.Value); IgnoreRoute(routes, ignoreItem.Url, ignoreConstraints); } // Manipluate the Routing Table foreach (RoutingItem routingItem in section.Map) { RouteValueDictionary defaults = new RouteValueDictionary(); RouteValueDictionary constraints = new RouteValueDictionary(); if (routingItem.Controller != string.Empty) defaults.Add("controller", routingItem.Controller); if (routingItem.Action != string.Empty) defaults.Add("action", routingItem.Action); foreach (Parameter param in routingItem.Paramaters) { defaults.Add(param.Name, param.Value); if (!string.IsNullOrEmpty(param.Constraint)) { constraints.Add(param.Name, param.Constraint); } } MapRoute(routes, routingItem.Name, routingItem.Url, defaults, constraints); }}
Add the following MvcRouteConfigurationSection in configSections configuration section of web. config:
<section name="RouteConfiguration" type="MvcXmlRouting.MvcRouteConfigurationSection"/>
Finally, use the MapRoute extension method we just wrote in Global. asax. cs to register our Route rules:
public class MvcApplication : System.Web.HttpApplication{ public static void RegisterRoutes(RouteCollection routes) { MvcRouteConfigurationSection section = (MvcRouteConfigurationSection)ConfigurationManager.GetSection("RouteConfiguration"); routes.MapRoute(section); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); }}
OK. Enjoy! By Q. Lee. lulu.
For the original article, see ASP. net mvc Routing Using XML Custom Configuration Settings.
The example code in this article adds constraints support based on the original code!
Download the sample code in this article: XmlRout.Web.rar