我試圖創建一個GET請求,它可以處理一些可選的搜索/過濾器引數。
請求應該是這樣的:
https://localhost:99999/api/computers?ModelName=asus&RAM=2& ScreenSize=22
或者
https://localhost:99999/api/computers?ModelName=asus&RAM=2
或者
https://localhost:99999/api/computers?RAM=2&螢屏尺寸=22
或者
https://localhost:99999/api/computers
重點是,它可以有多個可選引數,如。ModelName、RAM、ScreenSize或只是RAM、ScreenSize或ModelName、RAM或空。
該滾動器看起來像:
該滾動器看起來像:
[HttpGet]
public IHttpActionResult GetComupters(string ModelName。int? RAM, int? 螢屏尺寸)。
{
return Ok();
}
我應該如何為這種型別的搜索過濾建立RouteTemplate?
謝謝你!
uj5u.com熱心網友回復:
只要有一個默認的路由就夠了。你不需要任何額外的東西。你可以添加屬性路由。
[HttpGet("~/api/computers")/span>]
public IHttpActionResult GetComupters(string ModelName。int? RAM, int? 螢屏尺寸)
在net core中測驗,它可以正常作業。
但是如果你使用舊的net版本,你的注冊路由可能看起來像這樣
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")。
routes.MapMvcAttributeRoutes()。
routes.MapRoute()
name: "Default",
url: "api/{controller}/{action}/{id}"。
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324061.html
標籤:
上一篇:使用lambda(pd.DataFrame)進行元素操作
下一篇:無法識別事件訊息
