Here, we use the parameters in routes. MapRoute (parameter) for processing. The MapRoute overload is as follows:
RouteTable.Routes.MapRoute( string name, string url);RouteTable.Routes.MapRoute( string name, string url, object defaults);RouteTable.Routes.MapRoute( string name, string url, string[] namespaces);RouteTable.Routes.MapRoute( string name, string url, object defaults, object constraints);RouteTable.Routes.MapRoute( string name, string url, object defaults, string[] namespaces);RouteTable.Routes.MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);
Name parameter: the rule name can be retrieved at will, but cannot be duplicated. Otherwise, an error is returned. There is already a "default" route in the route set. The route name must be unique.
Url parameter: the rule for the url to obtain data. This is not a regular expression. You can include the parameters to be identified, for example, {controller}/{action}
Note: at least one Routing rule can be created by passing the name and url parameters. For example, the instance can be changed to: RouteTable. Routes. MapRoute ("Default", "{controller}/{action }");,
Defaults parameter: the default value in the url parameter. If a URL only has controller: localhost/home/, but we only create one url to obtain data rules: {controller}/{action }, in this case, the default value specified in the defaults parameter is set for the action parameter. ults is of the Object type, and an anonymous type can be passed to initialize the default value. (Action, that is, the index call method Userdetail ())
New {controller = "Home", action = "Userdetail "});
Constraints parameter: used to limit the rule of each parameter or the type of Http request. The constraints attribute is a RouteValueDictionary object, which is represented by a dictionary. The dictionary value can be, A string used to define a regular expression. The regular expression is case-insensitive. B. The object that implements the IRouteConstraint interface and contains the Match method. The regular expression can be used to specify the parameter format. For example, the controller parameter can only contain four digits: new {controller = @ "\ d {4 }"}
Public static void RegisterRoutes (RouteCollection routes) {// ignore the pair. the Route of the axd file, that is, directly accessing the file like WebForm. axd file routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); routes. ignoreRoute ("{resource }. ashx/{* pathInfo} "); routes. mapRoute ("Default", // Route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home ", action = "Index", id = UrlParameter. optional}, // Parameter defaults new { Controller = @ "^ \ B (?! GetRegSourceData) \ w * \ B $ "}); routes. Add (new ServiceRoute (" GetRegSourceData ", new WebServiceHostFactory (), typeof (GetRegSourceData )));}
View the code above, where new {controller = @ "^ \ B (?! GetRegSourceData) \ w * \ B $ "} filters out the URI whose Controller is GetRegSourceData.
Then match the Route of the WCF Rest Service.
routes.Add(new ServiceRoute("GetRegSourceData", new WebServiceHostFactory(), typeof(GetRegSourceData)));