1

I have a REST API controller in new .NET CORE app. If I make the following call: https://localhost/api/search?query=someText, I am able to execute the code below, so it looks like the routing is correct, however, query.query is always null. I would expect the value to be "someText".

What is wrong here?

[HttpGet]
public IActionResult Get([FromQuery]SearchQuery query)
{
} 

public class SearchQuery
    {
        [FromQuery(Name = "query")]
        public string query { get; set; }

    }
1
  • Please try changing the parameter name in your Get action method from SearchQuery query to SearchQuery model to avoid ambiguity with the property name inside your model. Commented Jun 9, 2019 at 1:33

1 Answer 1

4

The problem is that your parameters names conflict causing an ambiguity, because they are all query.

If you have it like this, it will work:

[HttpGet]
public IActionResult Get([FromQuery]SearchQuery queryParam)
{
  // here you can use queryParam.query
} 

public class SearchQuery
{
    [FromQuery(Name = "query")]
    public string query { get; set; }

}

You can also made it to work changing your parameter [FromQuery(Name = "query")] name to something else than query, but in this case you will need to change your request query string according.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.