In-depth research on ASP coding and final solutions

Source: Internet
Author: User

ASP garbled code is really tricky. This description is more authoritative. To be studied. Which materials are not as authoritative as official materials. Today, I finally selected a solution to the ASP encoding problem from MSDN.
... ASP garbled code is really tricky. This description is more authoritative. To be studied.

Which materials are not as authoritative as official materials. Today, I finally selected a solution to the ASP encoding problem from MSDN.

The following is a passage in MSDN.

Setting@ CODEPAGEExplicitly affects literal strings in a single response.Response. CodePageAffects dynamic strings in a single response, andSession. CodePageAffects dynamic strings in all responses in a session.

This sentence is clearly explained.@ CODEPAGE,Response. CodePage, Session. CodePageWhat are their respective roles.

@ CODEPAGEApply to all static strings, such as const blogname = "my home" in a file"

Response. CodePage,Session. CodePageApply to all dynamic output strings, such as <% = blogname %>

It is very important to explain this sentence.Response. CodePage is usedA single response, andSession. CodePageAll responses in a session.

Let's look at another sentence.

IfResponse. CodePageIs not explicitly set in a page, it is implicitly set by Session. CodePage,If sessions are enabled.If sessions are not enabled,Response. CodePageIs set@ CodePage, If @ CodePage is present in the page. If there is no@ CodePageIn the page, Response. CodePage is set byAspCodePageMetabase property. IfAspCodePageMetabase property is not set, or set to 0, Response. CodePage is set by the system ANSI code page.

At first glance, I understand the meaning as follows: when sessions is enabled, if Response. CodePage is not declared, Response. CodePage will be assigned a value by Session. CodePage. If sessions are not enabled, if @ CodePage has been declared, Response. CodePage will be assigned a value by @ CodePage, and so on .............

This sentence explains why some other pages such as oblog and z-blog are prone to garbled characters After SXNA is released, because other programs do not declare Response. codePage, and SXNA declares the Session. codePage, so when you enter SXNA, Session. codePage is assigned immediately (different versions, some versions are assigned 936 and some versions are assigned 65001 ),Then, when entering other programs, Response. CodePage is immediately assigned a value by Session. CodePage.,If Response. CodePage and page encoding are differentThe page will contain garbled characters. So I checked the Session when I entered the z-blog with garbled characters. codePage and Response. all codepages are 936, and when the oblog is garbled, the Session. codePage and Response. all codepages are 65001. that is to say, to ensure that the leaves are not garbled, we should declare Response. codePage, otherwise it will follow the Session. codePage to interpret the webpage (instead of interpreting the webpage according to @ codepage ).

I am actually confused if I just follow the above explanation, because we all use a Chinese operating system. Every time you enter the browser, you can try to output the Session. codePage, we can see that all of them are 936! Why didn't he assign 936 of the default Session. CodePage to Response. CodePage when entering Z-blog? Instead, I gave @ CodePage to Response. CodePage? Under what circumstances will Session. CodePage be assigned to Response. CodePage? In the original article, how should sessions are enabled be understood?

Perhaps the above should be understood as follows:

When Session. CodePage is declared by any program, if Response. CodePage is not declared, Response. CodePage is assigned a value by Session. CodePage. If the Session. when CodePage is not declared by any program, if @ CodePage has been declared, then Response. codePage will be assigned a value by @ CodePage ,...., the dynamic content part of the final page follows Response. codePage value explanation.

Because Zblog and Oblog both declare @ CodePage, when the user just starts the machine and then enters the browser to browse Zblog and Oblog, the Response. CodePage will be assigned a value by @ CodePage, so the foliar display is normal.

This sentence further explains the cause of garbled characters.

If you setResponse. CodePageOrSession. CodePageExplicitly, do so before sending non-literal strings to the client. If you use literal and non-literal strings in the same page, make sure the code page@ CODEPAGEMatches the code pageResponse. CodePage, Or the literal strings are encoded differently from the non-literal strings and display incorrectly.

One useful sentence is that ifResponse. CodePageAnd@ CODEPAGEOtherwise, garbled characters are generated. That is, when the Z-blog@ CODEPAGE= 65001 while Z-blog's Response. CodePage isSession. CodePageIf it is set to 936, garbled characters will occur, and oblog and vice versa.

I don't know whether the above explanation is clear-_-|

The following explains why SXNA sometimes assigns Session. CodePage to 936. One of my versions is written like this:

<% OriginalCodePage = Session. CodePage %>

.......

<% Session. CodePage = OriginalCodePage %>

When the user enters the browser, the default Session. CodePage is 936. In this case, the default 936 is not declared by the program, so it will not be assignedResponse. CodePageWhen you enter SXNA,Session. CodePageThe code above turned into a program declaration.Session. CodePage = 936So when I enter Zblog, I will give 936Response. CodePage.

So far, all the reasons have been clearly analyzed.

Therefore, to ensure that asp leaves will not appear garbled code should be like this: (assuming it is the UTF-8 leaves)

<% @ CODEPAGE = 65001%>

<% Response. CodePage = 65001%>

<% Response. Charset = "UTF-8" %>

Further explain why Response. Charset should be added, because MSDN says it should be added...

If the code page is set in a page, thenResponse. CharsetShoshould also be set.

In addition, the file encoding format should be the same as @ CODEPAGE:

The file format of a Web page must be the same as the @ CODEPAGE used in the page.

This is why zblog, pjblog, and other programs want to save files in UTF8 encoding format.

In summary, if all programs declare Response. CodePage, it will not be disturbed by Session. CodePage and garbled characters will occur. So Session. CodePage cannot be used easily!

References:

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-us/iissdk/html/268f1db1-9a36-4591-956b-d4249aeadcb0.asp

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-us/iissdk/html/582e6f47-52eb-413e-8b5d-c99145cb61d8.asp

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.