ASP. NET Web API practice series 03, routing templates, routing conventions, routing settings, asp. netapi

Source: Internet
Author: User

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.

 


Aspnet mvc is very wide and wide range of zookeeper a}/{B}/{c}/{d} € € f}
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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.