代码之家  ›  专栏  ›  技术社区  ›  4thSpace wkw

如何查询绑定参数?

  •  0
  • 4thSpace wkw  · 技术社区  · 8 年前

    我正在尝试查询绑定参数 id . 它一直通过 0 s2 提供时具有值:

    id=0,s=空
    http://localhost/api/values/123

    id=0,s2=真
    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";
        }
    

    为什么不是 身份证件 被抓获?

    2 回复  |  直到 8 年前
        1
  •  2
  •   Atul Chaudhary    8 年前

    将代码更改为使用FromRoute作为Id,因为它是通过route来的,并更改Http Get,它与您在url中传递的内容不一致

    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        [HttpGet()] 
        public string Get([FromRoute]int id, [FromQuery]string s2)
        {
            return "value";
        }
    }
    
        2
  •  0
  •   AJ -    8 年前

    您的代码应该是这样的

    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        [HttpGet("sub/{id?}")] 
        public string Get(int? id, string s2)
        {
            return "value";
        }
    }
    
    推荐文章