![](/img/trans.png)
[英]Web API 2 DataMember naming not working in FromUri parameters
[英]FromUri query string web api
我正在尝试使用 Web Api 从我的数据库中查询数据。 问题是当我向 Postman 发送 get 请求时出现错误。 我在我的程序中设置了断点,但该方法没有收到请求。
{
"Message": "The requested resource does not support http method 'GET'."
}
我尝试查询以下字符串:
https://localhost:44384/api/advertentie/search?location=Makarska&property=null&price=null&rooms=null&beds=null&baths=null
public class QueryModel
{
public string Location { get; set; }
public string Property { get; set; }
public decimal? Price { get; set; }
public int? Rooms { get; set; }
public int? Beds { get; set; }
public int? Baths { get; set; }
}
[HttpGet]
[Route("api/advertentie/search{Location}/{Property}/{Price}/{Rooms}/{Beds}/{Baths}")]
public IHttpActionResult Search([FromUri] QueryModel query)
{
}
正如@mason 指出的,您正在使用两种不同的 URL 方案。 如果要使用查询参数,则应使用[FromQuery]
。 然后您可以将您的路线更改为简单的[Route("api/advertentie/search")]
。 最终结果看起来像这样
[HttpGet]
[Route("api/advertentie/search")]
public IHttpActionResult Search([FromQuery] QueryModel query)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.