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

How to Fully Decode URL Parameters in C#?

Patricia Arquette
Release: 2025-01-06 15:19:41
Original
547 people have browsed it

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);
Copy after login

Method 2: HttpUtility.UrlDecode

string decodedUrl = HttpUtility.UrlDecode(encodedUrl);
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template