4thSpace
4thSpace

Reputation: 44352

How to query bind parameter?

I'm trying to query bind the parameter id. It keeps coming through as 0. s2 has a value when supplied:

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";
    }

Why isn't id being captured?

Upvotes: 0

Views: 250

Answers (2)

AJ -
AJ -

Reputation: 631

Your code should be like this

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

Upvotes: 0

Atul Chaudhary
Atul Chaudhary

Reputation: 3736

Change your code to use FromRoute for an Id as it is coming via route and change Http Get as well, it is not aligned with what you passing in url

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpGet()] 
    public string Get([FromRoute]int id, [FromQuery]string s2)
    {
        return "value";
    }
}

Upvotes: 2

Related Questions