Urlencode in ASP. NET should use URI. escapedatastring ()

Source: Internet
Author: User

Today, eggplant _ 2008 gave feedback on "C ++" in his blog"
The tag is invalid. After checking the code, httputility. urlencode (URL) is used to generate the link, and
Httputility. urldecode (URL), from encode to decode, "C ++" is changed to "C
"(The plus sign is changed to a space ). This is a well-known problem. Here we will analyze this problem and provide a solution.

Let's take a look at the problem:

1. Original link:

http://www.cnblogs.com/xd502djj/tag/C++/

2. After httputility. urlencode, get:

http://www.cnblogs.com/xd502djj/tag/C%2b%2b/

3. Request. rawurl, and get:

http://www.cnblogs.com/xd502djj/tag/C++/

4. httputility. urldecode, and get:

http://www.cnblogs.com/xd502djj/tag/C  /

The above step 3rd has obtained the correct result. The urldecode in step 4th changes the plus sign to a space.

It seems that the solution is very simple. We did the same in the beginning by canceling the urldecode. After a while, some users reported "Windows
The phone tag becomes "Windows + phone ". As shown in the following figure, when httputility. urlencode is used, spaces are converted
You need to call urldecode to restore the plus sign to a space, and then add httputility. urldecode (forget the previous "C ++" label problem ). However
After that, the "C ++" tag becomes invalid again... in this way, it seems that there are many bugs and work is very busy. It is actually a bug...

Finally one day, we said "We can't do this anymore" and began to look for a solution:

Now that httputility. urlencode () cannot be used, check whether there are any alternatives in. net.

Httputility. urlpathencode () was first found (). Hey, it's useful. It's easy to solve the issue of "C ++" and space, but... Later I found that I couldn't fix "C #" And it didn't encode.

Find Uri. EscapeUriString (), which is the same as HttpUtility. UrlPathEncode.

Continue searching... finally found... Uri. EscapeDataString (), done! See the following test code:

public void UrlEncodeTest()
{
string url = "C++ C#";
Console.WriteLine(HttpUtility.UrlEncode(url));//C%2b%2b+C%23
Console.WriteLine(HttpUtility.UrlPathEncode(url));//C++%20C#
Console.WriteLine(Uri.EscapeUriString(url));//C++%20C#
Console.WriteLine(Uri.EscapeDataString(url));//C%2B%2B%20C%23
}

Note: The runtime environment is. net4.

Source: http://www.cnblogs.com/dudu/archive/2011/02/25/asp_net_UrlEncode.html

Today, eggplant _ 2008 gave feedback on "C ++" in his blog"
The tag is invalid. After checking the code, HttpUtility. UrlEncode (url) is used to generate the link, and
HttpUtility. UrlDecode (url), from Encode to Decode, "C ++" is changed to "C
"(The plus sign is changed to a space ). This is a well-known problem. Here we will analyze this problem and provide a solution.

Let's take a look at the problem:

1. Original link:

http://www.cnblogs.com/xd502djj/tag/C++/

2. After HttpUtility. UrlEncode, get:

http://www.cnblogs.com/xd502djj/tag/C%2b%2b/

3. Request. RawUrl, and get:

http://www.cnblogs.com/xd502djj/tag/C++/

4. HttpUtility. UrlDecode, and get:

http://www.cnblogs.com/xd502djj/tag/C  /

The above step 3rd has obtained the correct result. The UrlDecode in step 4th changes the plus sign to a space.

It seems that the solution is very simple. We did the same in the beginning by canceling the UrlDecode. After a while, some users reported "Windows
The Phone tag becomes "Windows + Phone ". As shown in the following figure, when HttpUtility. UrlEncode is used, spaces are converted
You need to call UrlDecode to restore the plus sign to a space, and then add HttpUtility. UrlDecode (forget the previous "C ++" label problem ). However
After that, the "C ++" tag becomes invalid again... in this way, it seems that there are many bugs and work is very busy. It is actually a Bug...

Finally one day, we said "We can't do this anymore" and began to look for a solution:

Now that HttpUtility. UrlEncode () cannot be used, check whether there are any alternatives in. NET.

HttpUtility. UrlPathEncode () was first found (). Hey, it's useful. It's easy to solve the issue of "C ++" and space, but... Later I found that I couldn't fix "C #" And it didn't encode.

Find Uri. EscapeUriString (), which is the same as HttpUtility. UrlPathEncode.

Continue searching... finally found... Uri. EscapeDataString (), done! See the following test code:

public void UrlEncodeTest()
{
string url = "C++ C#";
Console.WriteLine(HttpUtility.UrlEncode(url));//C%2b%2b+C%23
Console.WriteLine(HttpUtility.UrlPathEncode(url));//C++%20C#
Console.WriteLine(Uri.EscapeUriString(url));//C++%20C#
Console.WriteLine(Uri.EscapeDataString(url));//C%2B%2B%20C%23
}

Note: The runtime environment is. net4.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.