0

Is there a way to accept anything in the query string of a route in .NET Core API 2.X? For example, if I were to pass the following to the same action, but with different query strings, I would receive a response containing the query string.

  • localhost:PORT/api/values/echo?something=2&somethingelse=test
  • localhost:PORT/api/values/echo?word=hello-world

The response to the two requests would respectively be:

  • something=2&somethingelse=test
  • word=hello-world

I thought the attempt below would work, but I was mistaken.

        [HttpGet("echo")]
        public IActionResult Echo(dynamic query)
        {
            return Ok(query);
        }

1 Answer 1

1
    [HttpGet("echo")]
    public IActionResult Echo()
    {
        var query = Request.QueryString.ToString();
        return Ok(query);
    }

This is for you case. But generally I don't see any point in it, you may want to use a class instead.

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

1 Comment

So I'd use a parameterless action. I imagined there'd be an Action filter attribute or a model binder or something.

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.