Create a REST API with Attribute Routing in ASP.NET Web API 2

原文:http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing

作为Restful API设计的一个参考,将其记录下来。

控制器上设添加属性:

RoutePrefix("/api/books")

Action Example URI Code
Get a list of all books. /api/books Route("")
Get a book by ID. /api/books/1 Route("{id:int}")
Get the details of a book. /api/books/1/details Route("{id:int}/details")
Get a list of books by genre. /api/books/fantasy Route("{genre}")
Get a list of books by a particular author. /api/authors/1/books Route("~/api/authors/{id:int}/books")
Get a list of books by publication date. /api/books/date/2013-02-16 [Route("date/{pubdate:datetime:regex(d{4}-d{2}-d{2})}")]
Get a list of books by publication /api/books/date/2013/02/16 (alternate form) [Route("date/{*pubdate:datetime:regex(d{4}/d{2}/d{2})}")]
原文地址:https://www.cnblogs.com/pengzhen/p/5836053.html