How to pass a url variable in web api that contains / or a question mark?

Asked

Viewed 151 times

-3

  • https://stackoverflow.com/questions/20034227/how-to-match-web-api-2-route-with-forward-slashes-in-request-parameters I tried something like this but also not

  • https://imgur.com/6amNLXC

1 answer

2

? and / are characters reserved in a URL.

You will need to encoding these characters:

? stays %3F and / stays %2F.

Note that if your parameter should be in the query string, you are setting the route wrong.

You should use the attribute [FromRoute]. Thus:

[HttpGet("{test}")]
public IActionResult Get([FromRoute] string test)
{
    return Ok(test);
}
  • %2F does not work but %3F worked

  • @Amadeuantunes This is how you are testing that you have some problem. I just tested by Postman and it works as expected.

  • As I said, there is another problem. https://imgur.com/a/lS0nw3g

  • https://stackoverflow.com/questions/20034227/how-to-match-web-api-2-route-with-forward-slashes-in-request-parameters I tried something like this but it didn’t work either

  • https://imgur.com/6amNLXC

  • @Amadeuantunes this parameter should be a querystring. Right?

  • @Amadeuantunes See my edition.

  • I updated the code but it doesn’t work anyway I have to make some configuration on the routes?

  • @Amadeuantunes Make sure that the test is right.

  • I did it the same way and not yet

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.