Home >Backend Development >C++ >How to Fully Decode URL Parameters in C#?

How to Fully Decode URL Parameters in C#?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-06 15:19:41556browse

How to Fully Decode URL Parameters in C#?

Decoding URL Parameters in C#

HTTP requests often contain URL parameters that may be encoded for security reasons. To access these parameters in C#, you need to decode them first.

Method 1: Uri.UnescapeDataString

string encodedUrl = "my.aspx?val=%2Fxyz2F";
string decodedUrl = Uri.UnescapeDataString(encodedUrl);

Method 2: HttpUtility.UrlDecode

string decodedUrl = HttpUtility.UrlDecode(encodedUrl);

Both methods perform basic URL decoding, but a single call might not be sufficient. To fully decode the URL, you can use a while loop to repeatedly decode it until no further changes occur:

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

With this method, the provided URL would be fully decoded to "my.aspx?val=/xyz2F".

The above is the detailed content of How to Fully Decode URL Parameters in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn