Dastan
Dastan

Reputation: 59

ASP.NET - %2F (forward slash) percent-encoded character in route parameter does not get decoded

I have the following simple controller that just takes a string in the route parameter and returns it as is:

[HttpGet("string/{value}")]
public ActionResult Echo([FromRoute] string value)
{
    return new JsonResult(new { output = value });
}

It works fine when I provide any percent-encoded character except "%2F" in the string, i.e. it returns it's corresponding string representation:

/string/%21%23%28 -> { "output": "!#(" }

However, for %2F, it seems it returns the percent-encoded string itself, rather than the corresponding forward-slash character, i.e. "/":

/string/%2F -> { "output": "%2F" }

How can I ask it to return "/" in the output instead of "%2F"?

Upvotes: 2

Views: 2013

Answers (2)

domshyra
domshyra

Reputation: 952

While this is an old question, you can use HttpUtility.UrlDecode(value) to get it to decode those things it encodes on your controller endpoint.

Here is a link to a more indepth answer for a similar question https://stackoverflow.com/a/46255640/759645

Upvotes: 0

Serge
Serge

Reputation: 43959

try this

value=value.Replace("%2F","/")
return new JsonResult(new { output =  value });

Upvotes: -1

Related Questions