ASP.NET Core:用法和URL格式
我试图在我的网络API中使用[FromQuery],我不知道如何使用它。
这是控制器中的GetAllBooks()方法:
[HttpGet] [Route("api/v1/ShelfID/{shelfID}/BookCollection")] public async Task GetAllBooks(string shelfID, [FromQuery] Book bookinfo) { //do something }
这是Book模型类:
public class Book { public string ID{ get; set; } public string Name{ get; set; } public string Author { get; set; } public string PublishDate { get; set; } }
我很困惑这是否是使用[FromQuery]的正确方法。 我以为URL是
https://localhost:xxxxx/api/v1/ShelfID/{shelfID}/BookCollection/IActionResult?ID="123"&Name="HarryPotter"
但是断点并没有达到我的控制器方法,所以我想也许URL不正确。 有什么建议? 谢谢!
当您通过类似属性明确定义路由时,将完全忽略方法和返回类型的名称。 IActionResult
不应该在那里。
正确的URL将是: https://localhost:xxxxx/api/v1/ShelfID/{shelfID}/BookCollection?ID="123"&Name="HarryPotter"
此外,查询字符串绑定仅对原始类型(字符串,整数等)开箱即用。 要将类绑定到查询字符串,您需要一个非常复杂的自定义模型绑定器。
最好只显式声明要传入的属性:
上述就是C#学习教程:ASP.NET Core:用法和URL格式分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public async Task GetAllBooks(string shelfID, [FromQuery] string ID, [FromQuery] string Name)
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1012978.html