Classic ASP and UTF-8
Asked Answered
S

3

7

I'm changing my application to work with utf-8 pages. So every ASP page has this code

Response.CodePage = 65001 
Response.CharSet = "utf-8"

And HTML

<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

I saved all documents in Visual Studio 2013 with encode UTF-8 (without signature) 65001.

This is working fine when I write special characters in HTML like this:

<p>Atenção</p>

But when I write in VBScript (classic ASP) it's not working and the special characters are messy. I can fix them by saving the document (.asp) with encode UTF-8 (with signature) 65001.

So, my questions are:

  • Do I have to use this encoding (with signature) on every page?
  • What kind of problems could I have with it?
Skysweeper answered 20/7, 2015 at 19:6 Comment(3)
I recommend reading this - hanselman.com/blog/InternationalizationAndClassicASP.aspx VS appears to call the byte order mark the signature.Haldeman
So, my ASP pages must have BOM.Skysweeper
It shouldn't do any harm to include the BOM. That said, I've often saved pages as UTF8 without BOM and I've had no difficulties. (I tend not to use VS for Classic ASP though, I use an editor called EditPlus, which is similar to Notepad2 as mentioned by Hanselman)Haldeman
S
7

You need to set the @Codepage directive for each .asp file. We are using a generic #include file that is included first on every page and has the following lines up front:

<%@Codepage = 65001 %>
<% Option explicit %>
<% Response.Codepage = 65001 %>

See more info about the Codepage directive here (Remarks section). The linked page is about Session.Codepage which might also get interesting for you if you want to use the built-in Session.

Saving all files with BOM is not a requirement for IIS, we have all files saved without BOM working properly.

A note from my experience, after working with ASP for many a year: we had sometimes problems with BOMs of source files sneaking into the generated output, which lead to problems in AJAX/JSON responses. The remedy was to use a Response.Clear before writing output.

Simulacrum answered 10/11, 2015 at 10:10 Comment(1)
As long as you specify <%@Codepage = 65001 %> the IIS ASP preprocessor understands it needs to treat the ASP page as UTF-8 whether it has a BOM or not.Laconia
M
1

I had the strange characters issue, tried all the suggested encoding settings suggested and nothing was working, although the above did get the data into the sql database correctly it wouldn't then display correctly (still an encoding/decoding issue).

My resolution was to remove the "CharSet=utf8;" from the database connection string.

So instead of my connection string of:

sConnection = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;CharSet=utf8;Port:3306;DATABASE=dbname;UID=uid;PASSWORD=*****;OPTION=3" 

I used:

sConnection = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;Port:3306;DATABASE=dbname;UID=uid;PASSWORD=*****;OPTION=3" 

Things are now displaying correctly!

Mickeymicki answered 27/3, 2017 at 12:25 Comment(0)
K
0

I appreciate this thread is perhaps a bit old/obsolete but just in case someone is still stuck with this problem, you need the following code to get the correct output.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Atenção</p>
</body>
<html>
Kacykaczer answered 17/5 at 7:51 Comment(1)
Without the following line, you get "Atenção" <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />Kacykaczer

© 2022 - 2024 — McMap. All rights reserved.