Summary of various garbled issues in ASP. NET, asp.net garbled

Source: Internet
Author: User

Summary of various garbled issues in ASP. NET, asp.net garbled

I often find that people are troubled by garbled code, and I feel lucky and seldom worry about it.

In this blog, I will summarize some of my experiences related to Garbled text for your reference.

Garbled text on the page

In a website, some pages are normally displayed, but some pages are garbled. In this case, check the web. config and file encoding.

If web. config is configured as follows:

<globalization fileEncoding="utf-8" />

The file encoding is not UTF-8:

Then there will be garbled issues.

Note:Otherwise, there will be garbled characters.

1. If fileEncoding is not set, no gibberish occurs.

2. fileEncoding = "gb2312". The file is UTF-8 encoded and will not be garbled.

As a result, I recommend that you save all files in UTF-8 encoding to solve this type of garbled problem.

Data garbled by AJAX

AJAX technology has been popular for so many years. I think few websites do not use this technology now. However, some people encounter garbled characters when using AJAX.

By analyzing this type of garbled case, I found that almost all data is submitted to the server in this way: "key1 =" + escape (value1) + "& key2 =" + escape (value2)

This method works normally in most cases, but it won't work if you encounter some special characters. I will explain the cause later.

I have prepared an example for this type of incorrect method (to keep the example simple, I will demonstrate a spliced URL ),

The page code is as follows:

<P> <a id = "link2" href = "#" target = "_ blank"> escape </a> </p> <script type = "text/javascript"> var str = "aa = 1 & bb =" + escape ("fish li + is me. ") +" & cc = "+ escape (" Daming Dynasty 1368 "); $ (" # link2 "). attr ("href", "/test_url_decode.ashx? Method = escape & "+ str); </script>

The server code reads those parameter values from QueryString and then outputs them. The code is too simple to be pasted out. (Sample code that can be downloaded)

When I click the link, the server returns the following result:

Note: the plus sign in the middle of "fish li + is me." does not exist.

There is a simple way to solve this problem, that is, using the $. param () method of JQuery. The modified code is as follows:

<Script type = "text/javascript"> var myobject = {aa: 1, bb: "fish li + is me. ", cc:" Daming Dynasty 1368 "};$ (" # link1 "). attr ("href", "/test_url_decode.ashx? Method = param & "+ $. param (myobject); </script>

In addition, I am very disgusted with splicing such submitted data: "key1 =" + escape (value1) + "& key2 =" + escape (value2)

Because the code is too readable, I suggest you use the $. AJAX method of JQuery to submit data to the server when calling ajax.

See the following sample code (note that I assign values to the data attribute ):

<P> <a id = "btnTestParam" href = "javascript: void (0);"> Click me! [Click me] </a> </p> <div id = "divResult"> </div> <script type = "text/javascript" >$ (function () {$ ("# btnTestParam "). click (function () {$. ajax ({url: "/TestParam. ashx ", type:" GET ", cache: false, data: {id: 2, name:" fish li + is me. ", tel :"~! @ # $ % ^ & * () _ +-= <>? | "," X? X! X & x ":" aa = 2 & bb = 3 & cc = Chinese characters. ", // Special key name, value content is also special. Encoding: "Go to hell .? & :) ", Chinese key name:" Daming Dynasty 1368 "}, success: function (responseText) {$ ("# divResult" ).html (responseText) ;}); </script>

Running result:

The correct URL encoding method in JavaScript

After reading the previous example, have you ever wondered why JQuery can solve the problem that escape cannot solve?

For this question, I 'd like to take a look at the description of escape in MSDN ():

MSDN makes it clear that I do not need to explain it again.

However, I think someone may ask: What if I use POST to submit data? It does not pass through the URL.

Yes, the parameter is not included in the URL when the POST data is stored, but the URL encoding is still used.

The POST data is also URL encoded because the form can be submitted in GET mode, and the data will be submitted to the server through URL.

Therefore, the submitted data must be URL encoded.

Let's take a look at how $. ajax processes the data submission process:

Ajax: function (origSettings) {var s = jQuery. extend (true, {}, jQuery. ajaxSettings, origSettings );//............... remove irrelevant code // convert data if not already a string if (s. data & s. processData & typeof s. data! = "String") {// note the following call s. data = jQuery. param (s. data, s. traditional );}

Let's take a look at the implementation process of jQuery. param:

// Serialize an array of form elements or a set of // key/values into a query stringparam: function (a, traditional) {var s = []; //............... remove irrelevant code // If an array was passed in, assume that it is an array of form elements. if (jQuery. isArray (a) |. jquery) {// Serialize the form elements jQuery. each (a, function () {add (this. name, this. value);} else {//............... remove some non- Key code} // Return the resulting serialization return s. join ("&"). replace (r20, "+"); function add (key, value) {// If value is a function, invoke it and return its value = jQuery. isFunction (value )? Value (): value; s [s. length] = encodeURIComponent (key) + "=" + encodeURIComponent (value );}}

The core of this Code is the implementation of the add function, which internally calls the encodeURIComponent () function.

We should pay attention to JQuery's data processing method: encodeURIComponent (key) + "=" + encodeURIComponent (value );

JQuery also replaced % 20 with the + number at the end.

In the WEB development field, do you have to worry about JQuery's authority? So I think the JQuery method is correct.

From the implementation of JQuery, we can also see that encodeURI () is not recommended for encoding URL data.

Here, I want to explain why encodeURI is not recommended.

EncodeURI is used to encode the entire URL string, if a parameter value itself contains some special characters.

Example: key = "x? X/x & x ", value =" aa = 2 & bb = 3 & cc = Chinese characters. ", The result of this function will be incorrect.

It is usually used to encode URL paths that contain similar Chinese characters and is not suitable for processing URL parameters.

However, the directory name and file name in the URL path can be English characters, so encodeURI is generally unavailable.

The correct URL encoding method in ASP. NET

We have introduced three URL encoding methods in JavaScript. on the server side, ASP. NET has more URL encoding methods. Today, I also summarized the server encoding, because I found that some information on the Internet is also incorrect.

Three URL encoding methods are provided in ASP. NET: HttpUtility. UrlPathEncode, HttpUtility. UrlEncode, Server. UrlEncode
. NET framework also provides the System. Uri class, which also has some methods for URL Processing. For example, the EscapeUriString and EscapeDataString methods can be used for URL path and parameter encoding tasks.

Which of the following methods should I choose?

In ASP. in. NET, select HttpUtility for encoding query parameters. urlEncode (str). When splicing a URL, HttpUtility is used. urlEncode (key) + "=" + HttpUtility. urlEncode (value) method. To encode the path in the URL, use HttpUtility. UrlPathEncode ()

I will explain the reasons for the other methods not recommended earlier:

1. server. urlEncode: This method actually calls HttpUtility. urlEncode, but it will try to use Response. contentEncoding indicates the encoding format, but HttpUtility. urlEncode (str) always uses UTF-8 encoding. If you do not want to be entangled by character encoding, you should discard the Server. urlEncode, after all, UTF-8 encoding is a better choice.

2. although System. the two Uri encoding methods can also implement the URL encoding tasks we need. However, when ASP.. NET filling Request. queryString, Request. the decoding method used in Form is HttpUtility. urlDecode, So if you choose to use System. the Uri-related encoding method obviously cannot match the decoding method.

Summary of the correct URL encoding method

Because many coding functions (methods) are important, I think it is necessary to make a conclusion.

A complete URL can be simply considered to contain two parts: file path (including directory) and query parameter (QueryString)
When encoding, it must be processed separately.

When encoding the file path, select encodeURI, HttpUtility. UrlPathEncode.

EncodeURIComponent and HttpUtility. UrlEncode should be selected for encoding query parameters, and the splicing method should be: Encode (key) + "=" + Encode (value)

You must not splice the entire URL (including query parameters) first, and then consider which encoding method to choose.

Again: Using escape in JavaScript is definitely wrong.

Completely solve the garbled problem between encodeURIComponent () and GB2312

Previously I suggested using encodeURIComponent () in JavaScript to process committed data, however encodeURIComponent () uses UTF-8 encoding when encoding characters. For this reason, some people may say that the encoding method of my website is gb2312!

<globalization requestEncoding="gb2312" responseEncoding="gb2312" />

For this answer, I sometimes really do not want to continue: you can not change the website code into UTF-8?

Now, I have designed a method that can solve the problem of using encodeURIComponent () in GB1212 coding websites. The design idea of this method is straightforward: Since encodeURIComponent () is using UTF-8 encoding, so, we don't just tell the server, the client submitted data is UTF-8 encoding, at this time the server as long as the identification, according to The UTF-8 encoding to decode, the problem is solved.

The code is actually very simple. First, let's look at the client code.

$. Ajax ({// pay attention to the following line of code, which adds a custom request header beforeSend: function (xhr) {xhr. setRequestHeader ("x-charset", "UTF-8") ;}, url: "/TestParam. ashx ", type:" GET ", cache: false, data: {id: 2, name:" fish li + is me. ", tel :"~! @ # $ % ^ & * () _ +-= <>? | "," X? X! X & x ":" aa = 2 & bb = 3 & cc = Chinese characters. ", // Special key name, value content is also special. Encoding: "Go to hell .? & :) ", Chinese key name:" Daming Dynasty 1368 "}, success: function (responseText) {$ (" # divResult ").html (responseText );}});

Note: On the basis of the original code, I added only one line of code:

beforeSend: function(xhr) {  xhr.setRequestHeader("x-charset", "utf-8"); },

Let's look at the server code. I wrote an HttpModule to solve this problem in a unified manner.

Public class ContentEncodingModule: IHttpModule {public void Init (HttpApplication app) {app. beginRequest + = new EventHandler (app_BeginRequest);} void app_BeginRequest (object sender, EventArgs e) {HttpApplication app = (HttpApplication) sender; HttpWorkerRequest request = (IServiceProvider) app. context ). getService (typeof (HttpWorkerRequest) as HttpWorkerRequest); // Note: I have not used the app. request. header S ["x-charset"] // because most programs do not access it, it will always be null. // if I want to ask this set at this time, it will cause filling in it. // I think the filling Headers set is much more costly than the call below. // Therefore, reading request Headers directly through HttpWorkerRequest minimizes the performance loss. String charset = request. getUnknownRequestHeader ("x-charset"); if (string. compare (charset, "UTF-8", StringComparison. ordinalIgnoreCase) = 0) // ASP. NET will access Request when filling QueryString and Form. contentEncoding is the character encoding app used for decoding. request. contentEncoding = System. text. encoding. UTF8 ;}

After the transformation, the result is: unless the client explicitly adds the "x-charset" Request Header, it will still be processed in the original way. For the server code, no modification is required.

Note:

1. If all the website submissions are submitted using JQuery, you can set them in a unified manner. This is a function supported by JQuery.

2. If you use JQuery1.5 or a later version, you can also write it as headers: {"x-charset": "UTF-8 "}

3. Even after the website uses UTF-8 encoding, all code does not need to be modified.

Cookie garbled

Some time ago, someone asked me in a blog comment: the asp.net server writes Chinese cookies, and the js client reads garbled characters.

In fact, this problem is better solved by using HttpUtility. UrlEncode encoding when writing cookies, and then using decodeURIComponent on the client to convert the content back. Here, we recommend using the jquery. cookie. js plug-in to read and write cookies. The sample code is as follows (front-end ):

$(function() {  var cookie = $.cookie("TestJsRead");  $("#cookieValue").text(cookie);});

Server code:

Cookie = new HttpCookie ("TestJsRead", HttpUtility. UrlEncode ("Daming Dynasty 1368"); Response. Cookies. Add (cookie );

The download file name is garbled

Sometimes we need to dynamically create a file during the running of the program and ask the user to download the file generated during the running. However, sometimes the user may require the program to generate a default file name, it is convenient for them to save. At this point, we only need to set the response header Content-Disposition and give a default file name.

In general, as long as the default download file name is English and a number, the problem will never occur. However, sometimes users may require the default file to contain Chinese characters, and eventually the problem will happen. See the following code:

Public void ProcessRequest (HttpContext context) {byte [] fileContent = GetFileContent (); context. response. contentType = "application/octet-stream"; string downloadName = "clownfishperformance test result .xlsx"; string headerValue = string. format ("attachment; filename = \" {0} \ "", downloadName); context. response. addHeader ("Content-Disposition", headerValue); context. response. outputStream. write (fileContent, 0, fileContent. length );}

This code can run properly in FireFox, Opera, Safari, and Chrome. The download dialog box displayed in FireFox is what I expect:

Unfortunately, this is the case in my IE8:

For this garbled problem, we need to make some modifications to the Code:

String downloadName = "clownfishperformance test result .xlsx"; if (context. Request. Browser. Browser = "IE") downloadName = HttpUtility. UrlPathEncode (downloadName );

The file name displayed by IE is not garbled.

Note: My machine environment is Windows Server 2003 SP2. the browser versions used for testing are:

Garbled multi-language data

Another garbled problem is that the same program is used by users of multiple character sets (languages.
For example, if the program is in simplified Chinese, users in Traditional Chinese cannot save traditional Chinese characters (even if simplified Chinese characters can be properly displayed ).

When this phenomenon is found, you need to check the database field type, whether it is Unicode or UTF-8, because when the character set of the data field does not support multiple languages, garbled problem will inevitably occur.

When using SQL SERVER, we recommend that all fields that save text start with N, such as nvarchar and nchar, unless you explicitly know that you want to save the zip code or md5 value, it is necessary to use the char (xxx) data type. Similarly, in MySQL, I suggest using a UTF-8

Summary of garbled characters

The garbled ASP. NET issue is generally related to two factors:

1. Select an inappropriate character encoding, for example, gb2312.

2. incorrect URL encoding method is selected, for example, escape ()

Therefore, the solution is not difficult:

1. Select UTF-8 for character encoding, including file encoding, request/response encoding, and database field type.

2. Select encodeURIComponent as the URL encoding method. We strongly recommend that you directly use JQuery.

I always think that the correct method can help me avoid many problems virtually.

If you are still worried about Garbled text, I suggest you first consider whether you have chosen incorrect encoding (method ).

Click here to download the sample code: demo

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.