Solution to garbled Request Parameters in ASP. NET

Source: Internet
Author: User

Asp tutorial. net request receiving parameter garbled Solution

In the asp.net tutorial, there are many attributes and methods for the request object. Several Common examples are: useragent returns the version information of the client browser, and userhostaddress returns the Host ip address of the remote client machine, userhostname returns the dns name of the remote client machine, and physicalapplicationpath returns the real path of the current request webpage on the server.

The following is the question: In my web. config configuration file, the global encoding is "gb2312:

<Globalization requestencoding = "gb2312" responseencoding = "gb2312" fileencoding = "gb2312" culture = "zh-cn"/>

When the current station submits "Chinese Text", garbled characters are received in the background using request. querystring ["xxx.

Decoding of system. web. httputility. urldecode ("xxx", "encoding type") is invalid.


Principles:

1: When the client url parameter is submitted, ext. js will encode it and then submit it. The client encoding is UTF-8 by default.

The client has three encoding functions by default: escape () encodeuri () encodeuricomponent ()
 

2: Why does garbled Code appear when request. querystring ["xxx"] is used to receive parameters?

To solve this problem, we must unbind the original processing logic of request. querystring.


Cut-in: querystringencoding

Internal encoding querystringencoding
{
Get
{
Encoding contentencoding = this. contentencoding;
If (! Contentencoding. equals (encoding. unicode ))
{
Return contentencoding;
}
Return encoding. utf8;
}
}

According to the querystringencoding code, the system uses the globalization configuration node encoding method by default. If the node fails to be obtained, the UTF-8 encoding method is used by default.
Conclusion:

When the client js submits the request to the server using UTF-8 encoding and receives the request. querystring, it first decodes the request with the gb2312 configured in globalization. As a result, garbled characters are generated.

Since:

1: js encoding method for urt-8

2: The default value gb2312 is configured on the server.

3: request. querystring calls httputility. urldecode by default to decode the received parameters.


Solution

After understanding the principles, there are also a variety of solutions:

1: UTF-8 encoding is unified globally, saving time and worry.
 

2: When gb2312 encoding is specified globally, the url contains Chinese characters. js is not encoded, such as the ext. js framework.

This method can only be specially processed. You can specify encoding and decoding on the server,
By default, the system calls httputility. urldecode ("xxx", the encoding configured by the system ),
So you call httputility. urlencode ("xxx", the encoding configured by the System) again and return to the original urt-8 encoding Parameter
Use httputility. urldecode ("xxx", UTF-8) to decode.

5. Other Instructions: The uri attribute is also included in the default decoding, while request. rawurl is the original parameter.

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.