如何通过URL零件或Querystring绑定WebAPI控制器操作参数
我的API方法是这样定义的:
[Route("api/[controller]")]
[ApiController]
public class ItemsController : ControllerBase
{
[HttpGet]
[HttpGet("{itemType}/{itemId}")]
public IActionResult Get(string itemType, string itemId)
{
return Ok($"{itemType}/{itemId}");
}
}
调用此作品:
/api/items/person/0123
通过 querystring 调用操作导致unprocessentity
/api/items?itemType=person&itemId=0123
我如何定义我的操作方法,以便它接受两种类型的输入?
I have an API method defined as such:
[Route("api/[controller]")]
[ApiController]
public class ItemsController : ControllerBase
{
[HttpGet]
[HttpGet("{itemType}/{itemId}")]
public IActionResult Get(string itemType, string itemId)
{
return Ok(quot;{itemType}/{itemId}");
}
}
Calling this works:
/api/items/person/0123
Calling the action by querystring results in UnprocessableEntity
/api/items?itemType=person&itemId=0123
How can I define my action method so that it accepts both types of input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试这样的尝试,以便您的第一个路由将允许您使用Querystring,并且第二个路由将允许通过属性路由将值绑定到变量。
you can try like this, so that the first route will allow you to use querystring and the second one will allow values to be bound to the variables through attribute routing.