asp.net URL contains Chinese parameters to create garbled solution _ practical skills

Source: Internet
Author: User
Problem:
Some time ago, in the system to do a similar link to the function block, has been running well, until one day add a similar link to the following address: Http://www.****.com/user.aspx?id= water days, there are big problems:
1, from the IE Address bar directly enter this address, access is correct;
2, do a static page, including this hyperlink, click Visit is also true;
3, is to add this link to this feature block, click on the access to receive there is garbled.
At first, by this problem also make head big, after a Google, finally is the problem to figure out, in fact, as long as the link address without any code transmission is not a problem. But it is added to this feature block, and then click, although this time in the IE Address bar shows the "Http://www.****.com/user.aspx?id= water days", but really pass the parameters of the past "water days" In fact, has been similar to the operation of Server.URLEncode, of course, the default is Utf-8 encoding, it is for this reason, if the other system does not carry out the corresponding decoding, direct operation of this parameter, resulting in the above error.
Workaround:
There are many ways to solve the internet, the most common is the following points:
1, in web.config <globalization requestencoding= "gb2312" responseencoding= "gb2312"/&gt, this method is indeed feasible, But I think a lot of people do not want to use this method to solve, the whole project request and response are set as gb2312, I think it is not a good way, a bit put the cart before the horse, after all, this will lead to many other problems arise.
2, to include the link to the ASPX document, modify its CharSet property to gb2312. This approach is acceptable, but not the best way to do this, because the feature block can be dynamically added to any page, do you want to modify all the pages? It's not quite right to think about it.
3, directly with Server.URLEncode and Server.decode for the encryption and decoding operations, this method for both the sender and receiver in a project is feasible, now the problem is that we have to go to other people's system, so we can only discard this method.
4, the use of Httputility.urlencode (query, System.Text.Encoding.GetEncoding ("GB2312"), the link in the value of the parameter gb2312 encoding, this method, The other party does not have any decoding operation, directly receive parameters will not appear garbled.
Most of the above are mentioned in the above 4 ways, others are similar to these. After careful analysis, think that the 4th method is the most suitable solution to the current problem, the Chinese parameters for example 4 of the operation. But there is another problem, because this is a user can freely enter the link address function, so the first thing to do is to first analyze these link URLs, parse out the parameters, and then 4 of these parameters in the operation, and then combined into the original URL address.
Fortunately, the URL parameter resolution C # already has class library support, no longer write those complex regular expressions to match. Here I do not say in detail, directly affixed to the original code:
Copy Code code as follows:

public static string Initchineseurl (String chineseurl)
{
Uri url = new Uri (Chineseurl);
System.Collections.Specialized.NameValueCollection NV = System.Web.HttpUtility.ParseQueryString (URL. Query, System.Text.Encoding.GetEncoding ("Utf-8"));
string query = "";
for (int i = 0; I < NV. Count; i++)
{
if (query). Trim () = = string. Empty)
{
query = "?" + NV. Keys[i] + "=" + Httputility.urlencode (Nv[i], System.Text.Encoding.GetEncoding ("GB2312"));
}
Else
{
Query + + "&" + NV. Keys[i] + "=" + Httputility.urlencode (Nv[i], System.Text.Encoding.GetEncoding ("GB2312"));
}
}
string u = Chineseurl.split ('? ') [0] + query;
return u;
}

Here I have all the parameters are gb2312 code operation, anyway, if the parameter is in English or a number, after these operations still unchanged.

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.