In ASP.NET, why is there UrlEncode() AND UrlPathEncode()?

后端 未结 2 1635
长发绾君心
长发绾君心 2020-12-17 00:14

In a recent project, I had the pleasure of troubleshooting a bug that involved images not loading when spaces were in the filename. I thought \"What a simple issue, I\'ll

2条回答
  •  我在风中等你
    2020-12-17 00:26

    UrlEncode is useful for use with a QueryString as browsers tend to use a + here in place of a space when submitting forms with the GET method.

    UrlPathEncode simply replaces all characters that cannot be used within a URL, such as <, > and .

    Both MSDN links include this quote:

    You can encode a URL using with the UrlEncode method or the UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.

提交回复
热议问题