What's the difference between EscapeUriString and EscapeDataString?

前端 未结 5 1301
旧时难觅i
旧时难觅i 2020-11-27 11:34

If only deal with url encoding, I should use EscapeUriString?

5条回答
  •  伪装坚强ぢ
    2020-11-27 11:55

    Comments in the source address the difference clearly. Why this info isn't brought forward via XML documentation comments is a mystery to me.

    EscapeUriString:

    This method will escape any character that is not a reserved or unreserved character, including percent signs. Note that EscapeUriString will also do not escape a '#' sign.

    EscapeDataString:

    This method will escape any character that is not an unreserved character, including percent signs.

    So the difference is in how they handle reserved characters. EscapeDataString escapes them; EscapeUriString does not.

    According to the RFC, the reserved characters are: :/?#[]@!$&'()*+,;=

    For completeness, the unreserved characters are alphanumeric and -._~

    Both methods escape characters that are neither reserved nor unreserved.

    I disagree with the general notion that EscapeUriString is evil. I think a method that escapes only illegal characters (such as spaces) and not reserved characters is useful. But it does have a quirk in how it handles the % character. Percent-encoded characters (% followed by 2 hex digits) are legal in a URI. I think EscapeUriString would be far more useful if it detected this pattern and avoided encoding % when it's immediately proceeded by 2 hex digits.

提交回复
热议问题