簡體   English   中英

FromUri 查詢字符串 web api

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM