简体   繁体   中英

Encoding string is differing with asp.net server.URL encoding when using '?' in the string

When I am using in aps.net to encode the string, the function I'm using is Server.UrlEncode(GAE?) , which returns the string GAE%3f .

When I am using javascript function to encode encodeURIComponent(GAE?) it is returning GAE%3F .

But when i validate the string it will never match since my validation failing. Requirement is there any way to encoded encodeURIComponent(GAE?) as GAE%3f as it is my requirement.

Other wise please let me know Server.UrlEncode code encoding it as capital 'F' this letter making difference. Note:Any way to get 'F' in small case when I use encodeURIComponent(GAE?) , The question mark ( ? ) encoded as %3F but it should be %3f

请尝试Uri.EscapeDataString("?")

I think the best approach would be to decode then do the comparison.

But if you have to do the comparison before that for any reason, this is the code you can use to change the encoded "special characters" to lower/upper:

string FixEncode(string val) {
    var result = new StringBuilder(val);
    int searchIndex = val.IndexOf('%');

    while (searchIndex >= 0)
    {
        result[searchIndex + 1] = char.ToUpper(result[searchIndex + 1]);
        result[searchIndex + 2] = char.ToUpper(result[searchIndex + 2]);
        searchIndex = val.IndexOf('%', searchIndex + 2);
    }

    return result.ToString();
}

You can run this function on both strings and then compare:

if (FixEncode(str1) == FixEncode(str2)) { /*...*/ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM