The default rule
In the ASP. NET MVC4
Code in Global.asax.cs code that does not have a default routing rule registered
The code is as follows:
public class WebApiApplication:System.Web.HttpApplication { protected void Application_Start () { Arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); } }
So where is the default routing rule registered?
We found
One more App_start folder in the project
Where the RoteConfig.cs file is the program that registers the default routing rule.
The code is as follows:
public static void RegisterRoutes (RouteCollection routes) { routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. Maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{id}", defaults:new {id = Routeparameter.optional} ); Routes. MapRoute ( name: "Default", URL: "{controller}/{action}/{id}", defaults:new {controller = "Home", Action = "Index", id = urlparameter.optional} );
There are two routing rules here
One is the routing rule for the API request
Another is the routing rule for common MVC page requests
The request path will access the WEBAPI function at the beginning of the string "API"
(Note: As for why use Maphttproute instead of Maproute, why use the routetemplate instead of the URL we introduce later chapters)
Because there is a {controller} in the Routetemplate
So requests for the API can be automatically mapped to the specified controller class
So how do you find the right action?
The system is judged by the way it is requested.
If the request was made in the form of a get
The action that starts with "Get" in the controller is matched
If the request is in the form of a post
Then the action that starts with "Post" in the controller is matched
If it is requested in a put way
The action that starts with "put" in the controller is matched
If the request is in the form of a delete
Then the action that starts with "Delete" in the controller is matched
In addition to these several default request methods
You can also define your own request mode on the client!
Routetemplate the most subsequent ID matches the argument in the action,
This is consistent with ASP.
Preliminary customization
So are all the actions that match get requests have to start with "get"?
No
We can use the method attribute to mark what a method belongs to and what the request
As follows:
[HttpGet] Public ienumerable<product> allproducts () { return products; }
The corresponding method characteristics are also
[HttpPost]
[Httpdelete]
[Httpput]
You can also use the following method attributes to differentiate
[Acceptverbs ("GET")]
[Acceptverbs ("GET", "HEAD")]
Plus, if you write a function in the controller,
Must be public and must start with get
And you don't want the client to request this action
Then you can use the method attribute
[Nonaction]
To mark this method
Further customization
We can still put the name of the action in the default request rule
Just change the routetemplate to the following:
Routes. Maphttproute ( name: "Actionapi", routetemplate: "Api/{controller}/{action}/{id}", defaults:new {id = Routeparameter.optional} );
ASP. NET WEB API routing rules (II)