ASP.NET Core: [FromQuery] usage and URL format

前端 未结 1 1309
面向向阳花
面向向阳花 2021-01-03 23:09

I am trying to use [FromQuery] in my web api and I am not sure how to use it.

Here\'s the GetAllBooks() method in the controller:

 [HttpGet]
 [Route(         


        
1条回答
  •  孤街浪徒
    2021-01-03 23:10

    The name of the method and return type are completely ignored when you define your route explicitly via attributes like that. IActionResult shouldn't be there.

    The correct URL would be: https://localhost:xxxxx/api/v1/ShelfID/{shelfID}/BookCollection?ID="123"&Name="HarryPotter"

    Furthermore, query string binding only works out of the box for primitive types (string, int, etc). To bind a class to a query string you'd need a custom modelbinder, which is quite involved.

    It would be better to just explicitly declare the properties you want to pass in:

    public async Task GetAllBooks(string shelfID, [FromQuery] string ID, [FromQuery] string Name)
    

    0 讨论(0)
提交回复
热议问题