Attribute Routing trong ASP.NET Web API 2.0 là gì?
Attribute Routing trong ASP.NET Web API 2.0 là gì?
Attribute Routing khai báo route trực tiếp bằng attribute trên controller/ action thay vì bảng route tập trung.
// Web API 2: bật trong WebApiConfig
config.MapHttpAttributeRoutes();
[RoutePrefix("api/authors")]
public class AuthorsController : ApiController
{
[Route("")] [HttpGet] public IEnumerable<Author> GetAll() => repo.All();
[Route("{id:int}")] [HttpGet] public IHttpActionResult Get(int id) => Ok(repo.Get(id));
[Route("{id:int}/books")] public IEnumerable<Book> Books(int id) => repo.BooksOf(id);
[Route("~/api/books/{isbn:length(13)}")] public Book ByIsbn(string isbn) => ...; // ~ bỏ qua prefix
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.