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!