asp.net – How do I decode a URL parameter using C#?

The Question :

152 people think this question is useful

How can I decode an encoded URL parameter using C#?

For example, take this URL:

my.aspx?val=%2Fxyz2F

The Question Comments :

The Answer 1

108 people think this answer is useful
Server.UrlDecode(xxxxxxxx)

The Answer 2

301 people think this answer is useful
string decodedUrl = Uri.UnescapeDataString(url)

or

string decodedUrl = HttpUtility.UrlDecode(url)

Url is not fully decoded with one call. To fully decode you can call one of this methods in a loop:

private static string DecodeUrlString(string url) {
    string newUrl;
    while ((newUrl = Uri.UnescapeDataString(url)) != url)
        url = newUrl;
    return newUrl;
}

The Answer 3

80 people think this answer is useful

Have you tried HttpServerUtility.UrlDecode or HttpUtility.UrlDecode?

The Answer 4

23 people think this answer is useful

Try this:

string decodedUrl = HttpUtility.UrlDecode("my.aspx?val=%2Fxyz2F");

The Answer 5

21 people think this answer is useful

Try:

var myUrl = "my.aspx?val=%2Fxyz2F";
var decodeUrl = System.Uri.UnescapeDataString(myUrl);

Add a Comment