[英]How to filter model data via query string params in asp.net mvc 4 ?
[英]ASP.NET MVC GET request with multiple optional filter query params
我正在尝试创建一个可以处理一些可选搜索/过滤参数的 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&ScreenSize=22
或者
https://localhost:99999/api/computers
关键是它可以有多个可选参数,例如:ModelName、RAM、ScreenSize 或只是 RAM、ScreenSize 或 ModelName、RAM 或空。
控制器看起来像:
[HttpGet]
public IHttpActionResult GetComupters(string ModelName, int? RAM, int? ScreenSize)
{
return Ok();
}
我应该如何为这种类型的搜索/过滤构建 RouteTemplate?
谢谢!!!
足够的默认路由。 你不需要任何额外的东西。 您可以添加属性路由。
[HttpGet("~/api/computers")]
public IHttpActionResult GetComupters(string ModelName, int? RAM, int? ScreenSize)
它在.net core 中进行了测试并且可以正常工作。
但是如果你使用 old.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 }
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.