我试图查询绑定参数id
。它一直持续到0
。提供时s2
有一个值:
id = 0,s = null
http://localhost/api/values/123
id = 0,s2 = true
http://localhost/api/values/123?s2=true
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet("sub/{id?}")]
public string Get([FromQuery]int id, string s2)
{
return "value";
}
为什么没有id
被捕获?
答案 0 :(得分:2)
将代码更改为使用FromRoute作为ID,因为它是通过路径传输并更改Http Get,它与您在url中传递的内容不一致
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet()]
public string Get([FromRoute]int id, [FromQuery]string s2)
{
return "value";
}
}
答案 1 :(得分:0)
您的代码应该是这样的
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet("sub/{id?}")]
public string Get(int? id, string s2)
{
return "value";
}
}