8

Tôi đang xây dựng một API cho một Twitter như trang web sử dụng Web API và gặp rắc rối với việc lập bản đồ các tuyến đườngLàm thế nào để lập bản đồ các tuyến đường WebAPI đúng

tôi có những hành động sau đây để điều khiển tài khoản:

public User Get(string firstname, string lastname) 
public User Get(Guid id) 
public User Friends(Guid id) 
public User Followers(Guid id) 
public User Favorites(Guid id) 

các tuyến mong muốn và các tài liệu được tạo ra nên là:

api/users?firstname={firstname}&lastname={lastname} 
api/users/{id} 
api/users/{id}/friends 
api/users/{id}/followers 
api/users/{id}/favorites 

Trong WebApiConfig.cs tôi có:

config.Routes.MapHttpRoute(
    "2", 
    "api/{controller}/{id}", 
    new { action = "get", id = RouteParameter.Optional } 
); 


config.Routes.MapHttpRoute(
    "1", 
    "api/{controller}/{id}/{action}" 
); 

Tôi có thể lập bản đồ các tuyến đường WebAPI chính xác như thế nào?

Trả lời

0

Có rất nhiều tài liệu tham khảo hữu ích về chủ đề này, chẳng hạn như:

Bạn đã có một cái nhìn tại các?

Cập nhật ..

thực hành tốt hơn của nó để rõ ràng nêu mà tham số là có, ví dụ:

config.Routes.MapHttpRoute(
     name: "2", 
     routeTemplate: "api/{controller}/{id}", 
     defaults: new { action = "Get", id = RouteParameter.Optional }, 
    ); 

    config.Routes.MapHttpRoute(
     name: "1", 
     routeTemplate: "api/{controller}/{action}/{id}", 
     defaults: null 
    ); 

điều chính tôi có thể thấy sai là bạn đã hành động/id theo thứ tự sai trong tuyến đường "1".

+0

xem tất cả các số – Donny

+0

Cập nhật câu trả lời của tôi – MikeDub

19

Với sự linh hoạt bạn muốn, bạn nên có một cái nhìn tại

Attribute Routing in ASP.NET Web API 2

Trong WebApiConfig.cs phép thuộc tính định tuyến như

// Web API routes 
config.MapHttpAttributeRoutes(); 

Trong UserController

Note trao tên hành động Friends, Followers and Favorites chúng ngụ ý trả lại ing bộ sưu tập chứ không phải là người dùng đơn lẻ

[RoutePrefix("api/users")] 
public class UserController: ApiController { 

    //eg: GET api/users?firstname={firstname}&lastname={lastname} 
    [HttpGet] 
    [Route("")] 
    public User Get([FromUri]string firstname,[FromUri] string lastname) {...} 

    //eg: GET api/users/{id} 
    [HttpGet] 
    [Route("{id:guid}")] 
    public User Get(Guid id){...} 

    //eg: GET api/users/{id}/friends 
    [HttpGet] 
    [Route("{id:guid}/friends")] 
    public IEnumerable<User> Friends(Guid id){...} 

    //eg: GET api/users/{id}/followers 
    [HttpGet] 
    [Route("{id:guid}/followers")] 
    public IEnumerable<User> Followers(Guid id){...} 

    //eg: GET api/users/{id}/favorites 
    [HttpGet] 
    [Route("{id:guid}/favorites")] 
    public IEnumerable<User> Favorites(Guid id){...} 
} 
5

Định tuyến có trật tự nhạy cảm. Trận đấu đầu tiên luôn thắng. Vì vậy, điều quan trọng là bạn đặt hàng cho bạn các tuyến đường từ cụ thể nhất đến ít cụ thể nhất.

// All parameters are required, or it won't match. 
// So it will only match URLs 4 segments in length 
// starting with /api. 
config.Routes.MapHttpRoute(
    "1", 
    "api/{controller}/{id}/{action}" 
); 

// Controller is required, id is optional. 
// So it will match any URL starting with 
// /api that is 2 or 3 segments in length. 
config.Routes.MapHttpRoute(
    "2", 
    "api/{controller}/{id}", 
    new { action = "get", id = RouteParameter.Optional } 
); 

Khi tuyến đường của bạn được sắp xếp theo cách này, bạn sẽ nhận được hành vi mà bạn mong đợi.

Các vấn đề liên quan