我試圖支持每個控制器的多個Get()方法,以及通過web api可訪問的特定命名方法。我已經在MVC 5中做了這個,但似乎無法弄清楚它是如何在MVC 6中完成的。任何想法?謝謝。MVC 6多種獲取方法
回答
您可以使用屬性路由鏈路上,這 -
[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}";
}
}
在這裏看到更多的細節 - http://nodogmablog.bryanhogan.net/2016/01/asp-net-5-web-api-controller-with-multiple-get-methods/
不能有多個具有相同url模式的Get方法。您可以使用屬性路由併爲不同的url模式設置多個GET方法。
[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;
}
}
你可以看到,我用兩個HttpGet
和Route
屬性來定義的路由模式。
通過上述配置,你會得到以下答覆
請求URL:yourSite/API /問題/
結果["value1","value2"]
請求的URL:yourSite/api/issues/4
結果request for 4
請求URL:yourSite/API/SPECIAL2/6
結果request for GetSpecial2 method with id:6
請求URL:yourSite /另一/ 3
結果request for AnotherOne method with id:3
真棒!非常感謝。 –
請看我的[問題](https://stackoverflow.com/questions/46680893/passing-multiple-parameters-to-web-api-get-method) – faisal1208
- 1. 一種方法,採取多種類型
- 2. UseSqlServer方法缺少MVC 6
- 3. Spring MVC獲取方法URL
- 4. JAva獲取類變量:多種方法或一種方法與開關
- 5. 多種方法
- 6. EF 6的哪種方法更適合從數據庫asyn中獲取數據?
- 7. 如何使用多種通用匹配方法獲取MethodInfo
- 8. 從一種方法獲取很多變量到另一個C#
- 9. 來自多種方法的NSURLConnection請求 - 獲取回調到入門方法
- 10. ASP 5 MVC 6 beta8,獲取會話使用剃刀(更好的方法)
- 11. Asp.NET MVC ModelBinder,獲取操作方法
- 12. Spring MVC:在方法中獲取RequestMapping值
- 13. MVC獲取所有操作方法
- 14. Web API - 多種POST方法
- 15. 通過另一種方法獲取方法的局部變量
- 16. 在Java中使用主要方法獲取某種方法
- 17. 從另一種方法的方法中獲取變量
- 18. 如何獲得使用多種POST方法的MVC RPC樣式API控制器
- 19. MVC Post方法沒有從get方法獲取任何信息?
- 20. 元編程 - 多種方法
- 21. 數組和多種方法
- 22. xcode多種主要方法
- 23. 多種方法NSNotificationCenter iPhone
- 24. 多種方法警告
- 25. 循環多種方法
- 26. Java多種方法錯誤
- 27. 多種測試方法
- 28. 多種方法或參數?
- 29. 代表和多種方法
- 30. 多種方法調用
謝謝!這是一個更好的解決方案,因爲我可以使用正常的嵌套路線。 –
請看我的[問題](https://stackoverflow.com/questions/46680893/passing-multiple-parameters-to-web-api-get-method) – faisal1208