ASP. NET Web API practice series 03, routing templates, routing conventions, routing settings, asp. netapi
The routes of ASP. NET Web APIs are similar to those of ASP. net mvc. routes are also placed in RouteTable. You can set a routing template in WebApiConfig. cs in the App_Start folder. The default routing template is:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
● The static fragment api is mainly used to differentiate ASP. net mvc routes.
● Why is there no {action }? By default, you can find the Action according to the Convention, as long as the Action name complies with the Convention.
● The placeholder variable {id} maps the parameter of Action.
□Routing by Convention
For GET, POST, PUT, and DELETE requests, if the Action name starts with Get, Post, Put, and Delete, This is the Convention, meaning that no Action is specified in the request url, you can route to the corresponding Action.
Suppose there is an api controller like this:
public class BooksController : ApiController
{
public void GetAllBooks(){}
public IEnumerable<Book> GetBookById(int id)
public HttpResponseMessage DeleteProduct(int id){}
}
● Input in the browser: api/books and Get request
The parameter is not included. Because the GetAllBooks name starts with Get, it complies with the Convention and will be mapped to the GetAllBooks method.
● Input in the browser: api/books/8 AND Get request
With parameters, it is mapped to the GetBookById (int id) method. The Web API assigns 8 of the string type to the parameter variable id of the int type.
● Input in the browser: api/books/8 and is a DELETE request
Eproduct (int id)
● Input in the browser: api/books and POST request
No Action corresponding to the POST request. The 404 status code is returned.
□Route by HTTP Method
The attributes of HttpGet, HttpPut, HttpPost, and HttpDelete can be attached to the Action.
public class BooksController : ApiController
{
[HttpGet]
public Book FindBook(id){}
}
If an Action allows multiple HTTP methods, the AcceptVerbs attribute is used.
public class BooksController ; ApiController
{
[AcceptVerbs("GET","HEAD")]
public Book FindProduct(id){}
}
□Action Routing
You can set the routing template in WebApiConfig. cs to take Action into account.
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
In the api controller:
public class BooksController : ApiController
{
[HttpGet]
public string Details(int id);
}
In this case, enter a format similar to "api/books/details/8" in the browser to map to the Details (int id) method.
You can also use the ActionName attribute to get an alias for the Action:
public class BooksController : ApiController
{
[ActionName("Sth")]
public HttpResposneMessage GetSth(int id);
}
If you do not want the Action to be included in the route, you can use the NoAction attribute.
public class BooksController : ApiController
{
[NonAction]
public string GetSomeData(){}
}
Summary: The routing templates defined in WebApiConfig. cs are all placed in RouteTable. At the Action level, if you want to route requests to the Action, you can use the Convention and Http Method attributes.
In aspnet mvc, if a route {a}/{B}/{c}/{d} is registered, how does one resolve the route?
Routes. mapRoute ("Default", // route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home ", action = "Index", id = UrlParameter. optional} // The default value of the parameter). Generally, the route is like this.
The one you wrote needs custom routing.
To define a route, you must first write the Controller name and method name.
For example
Routes. mapRoute ("Default2", // route name "{a}/{B}/{c}/{d }", // URL with parameters new {controller = "Home", action = "Index", a = "1", B = "2", c = "3 ", d = "4"} // The default value of the parameter). This route is relatively simple to write.
However, this route actually calls
Index () // four parameters under the Home Controller
I understand it in this way.
If you find something wrong, let's take a look.
Show you the routes I wrote
Routes. mapRoute ("caps2." San-Francisco-49ers-Hats ", new {controller =" PClass ", action =" Caps "}); routes. mapRoute ("Versions2", "San-Francisco-49ers-{Version}-Jersey-Page-{p}", new {controller = "PClass", action = "SiteVersion2 "}, new {Version = @ "(Elite | Limited | Game) $", p = @ "\ +? [1-9] [0-9] * "}); routes. mapRoute ("Versions", "San-Francisco-49ers-{Version}-Jersey", new {controller = "PClass", action = "SiteVer ...... remaining full text>
Problems with ASPNET Routing
Refer to this
System. Web. Routing. RouteTable. Routes. MapPageRoute ("product", "product/{id).htm ","~ /Aspx/product. aspx ", false, null,
New System. Web. Routing. RouteValueDictionary {"id", @ "\ d + "}},
New System. Web. Routing. RouteValueDictionary {"id", "id "}});
In addition, it is worth noting that. It does not pass parameters in the form of GET requests. Therefore, it must be used to obtain the id parameter on the product. aspx page.
String id = Request. RequestContext. RouteData. Values ["id"]. ToString ();