2016-01-18 15 views
13

Tôi đang cố gắng hỗ trợ nhiều phương thức Get() trên mỗi bộ điều khiển, cũng như chỉ các phương thức được đặt tên đặc biệt có thể truy cập thông qua api web. Tôi đã làm điều này trong MVC 5, nhưng dường như không thể tìm ra cách nó được thực hiện trong MVC 6. Bất kỳ ý tưởng? Cảm ơn.MVC 6 Nhiều phương thức nhận

Trả lời

15

Bạn có thể sử dụng thuộc tính định tuyến liên kết này -

[Route("api/[controller]")] /* this is the defualt prefix for all routes, see line 20 for overridding it */ 
public class ValuesController : Controller 
{ 
    [HttpGet] // this api/Values 
    public string Get() 
    { 
     return string.Format("Get: simple get"); 
    } 

    [Route("GetByAdminId")] /* this route becomes api/[controller]/GetByAdminId */ 
    public string GetByAdminId([FromQuery] int adminId) 
    { 
     return $"GetByAdminId: You passed in {adminId}"; 
    } 

    [Route("/someotherapi/[controller]/GetByMemberId")] /* note the/at the start, you need this to override the route at the controller level */ 
    public string GetByMemberId([FromQuery] int memberId) 
    { 
     return $"GetByMemberId: You passed in {memberId}"; 
    } 

    [HttpGet] 
    [Route("IsFirstNumberBigger")] /* this route becomes api/[controller]/IsFirstNumberBigger */ 
    public string IsFirstNumberBigger([FromQuery] int firstNum, int secondNum) 
    { 
     if (firstNum > secondNum) 
     { 
      return $"{firstNum} is bigger than {secondNum}"; 
     } 
     return $"{firstNum} is NOT bigger than {secondNum}"; 
    } 
} 

Xem ở đây để biết thêm chi tiết - http://nodogmablog.bryanhogan.net/2016/01/asp-net-5-web-api-controller-with-multiple-get-methods/

+0

Cảm ơn! Đây là một giải pháp tốt hơn, vì tôi có thể sử dụng các tuyến đường lồng nhau bình thường. –

+0

vui lòng xem [câu hỏi] của tôi (https://stackoverflow.com/questions/46680893/passing-multiple-parameters-to-web-api-get-method) – faisal1208

25

Bạn không thể có nhiều phương thức Nhận với cùng mẫu url. Bạn có thể sử dụng định tuyến thuộc tính và thiết lập nhiều phương thức GET cho các mẫu url khác nhau.

[Route("api/[controller]")] 
public class IssuesController : Controller 
{ 
    // GET: api/Issues 
    [HttpGet] 
    public IEnumerable<string> Get() 
    { 
     return new string[] { "item 1", "item 2" }; 
    } 

    // GET api/Issues/5 
    [HttpGet("{id}")] 
    public string Get(int id) 
    { 
     return "request for "+ id; 
    } 

    // GET api/Issues/special/5 
    [HttpGet("special/{id}")] 
    public string GetSpecial(int id) 
    { 
     return "special request for "+id; 
    } 
    // GET another/5 
    [HttpGet("~/another/{id}")] 
    public string AnotherOne(int id) 
    { 
     return "request for AnotherOne method with id:" + id; 
    } 
    // GET api/special2/5 
    [HttpGet()] 
    [Route("~/api/special2/{id}")] 
    public string GetSpecial2(int id) 
    { 
     return "request for GetSpecial2 method with id:" + id; 
    } 
} 

Bạn có thể thấy rằng tôi sử dụng cả hai HttpGetRoute thuộc tính để xác định các mô hình tuyến đường.

Với cấu hình trên, bạn bạn sẽ nhận được các câu trả lời dưới đây

Yêu cầu Url: yoursite/api/vấn đề/

quả ["value1","value2"]

Yêu cầu Url: yourSite/api/issues/4

quả request for 4

Yêu cầu Url: yoursite/api/special2/6

quả request for GetSpecial2 method with id:6

Yêu cầu Url: yoursite/khác/3

quả request for AnotherOne method with id:3

+1

Awesome! Cảm ơn bạn rất nhiều. –

+0

vui lòng xem [câu hỏi] của tôi (https://stackoverflow.com/questions/46680893/passing-multiple-parameters-to-web-api-get-method) – faisal1208

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