Encoding in ASP.NET sites

F

fossmo

Is there a way to force an encoding in a asp.net site?
I have tried to save the pages in utf-8 encoding, with a lot of
succsess.
Letters like ÆØÅ (norwegian letters) are displayed when I do it this
way, but I dont want to have to go throu every page and save it in
utf-8 encoding. Is there a way I can change this in only one place?
I have tried to set the globalization tag in the web.config file, but
with no succsess.

Pål
 
K

Kevin Spencer

See http://msdn2.microsoft.com/en-us/library/39d1w2xf(VS.80).aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

Is there a way to force an encoding in a asp.net site?
I have tried to save the pages in utf-8 encoding, with a lot of
succsess.
Letters like ÆØÅ (norwegian letters) are displayed when I do it this
way, but I dont want to have to go throu every page and save it in
utf-8 encoding. Is there a way I can change this in only one place?
I have tried to set the globalization tag in the web.config file, but
with no succsess.

Pål
 
P

PL

If you enter text directly onto the pages in UTF-8 then your pages must be
saved in UTF-8, and you must specify the charset in the header such as:

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

this has nothing to do specifically with ASP.NET

It's the same in any encoding, the problem is that for some reason UTF-8 is
still considered "extra" instead of the default so most editors still save
text as ASCII only unless otherwise specified.

Although you don't necessarily have to save your scripts as UTF-8 to display
UTF-8, if you have unicode text being pulled from a database then alll you
need is to specify the default encoding for your app which is usually
already set to utf-8, you should also specify CODEPAGE="65001" in your @
Page declaration as well as provide the Content-Type meta tag so browsers
know what encoding the page is in.

In short:

o If you enter text directly onto your pages in UTF-8 your pages must be
saved as UTF-8
o If you pull stuff from database your pages does not have to be saved as
UTF-8 but you still have to specify the codepage and content-type.

PL.


<[email protected]> skrev i meddelandet
Is there a way to force an encoding in a asp.net site?
I have tried to save the pages in utf-8 encoding, with a lot of
succsess.
Letters like ÆØÅ (norwegian letters) are displayed when I do it this
way, but I dont want to have to go throu every page and save it in
utf-8 encoding. Is there a way I can change this in only one place?
I have tried to set the globalization tag in the web.config file, but
with no succsess.

Pål
 
J

Joerg Jooss

Thus wrote PL,
If you enter text directly onto the pages in UTF-8 then your pages
must be saved in UTF-8,

Yes and...
and you must specify the charset in the header
such as:

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

No, you don't have to. The response encoding is set as charset attribute
as part of the Content-Type HTTP header, which overrules META tags in every
civilized browser in use (YMMV).

META tags are also completely useless for true multibyte encodings like UTF-16,
at least as far as IE and Mozilla are concerned.
Although you don't necessarily have to save your scripts as UTF-8 to
display UTF-8, if you have unicode text being pulled from a database
then alll you need is to specify the default encoding for your app
which is usually already set to utf-8, you should also specify
CODEPAGE="65001" in your @ Page declaration as well as provide the
Content-Type meta tag so browsers know what encoding the page is in.

There's also the ResponseEncoding attribute, which uses friendly IANA names
instead of Win32 codepage identifiers. And of course there's the ContentEncoding
property in HttpResponse to set the encoding through code.

But all of this isn't required, *if* requestEncoding and responseEncoding
are set to UTF-8 in web.config, which is the default.

Cheers,
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
Is there a way to force an encoding in a asp.net site?
I have tried to save the pages in utf-8 encoding, with a lot of
succsess.
Letters like ÆØÅ (norwegian letters) are displayed when I do it this
way, but I dont want to have to go throu every page and save it in
utf-8 encoding. Is there a way I can change this in only one place?
I have tried to set the globalization tag in the web.config file, but
with no succsess.

VS2005's approach to choose an encoding for a source file is a bit bizarre.
The default encoding for ASP.NET pages with code files (which happens to
be the platform's default encoding on my machines) is different from the
one for inline code ASP.NET pages (UTF-8 for me) -- at least for C# projects.
AFAIK the same is true for Web Services code.

I don't really know of a way to enforce a certain encoding for a newly created
source file :-(

Cheers,
 
P

PL

I don't remember saying "required" anywhere but I agree I should have said
"should" instead of must. You didnt really find any factual errors so why
bother answering at all when what I said is actually the more correct way to
do it.

If you exclude the meta tag some browsers do behave strangely and you also
assume it will always be browsers reading it, sometimes the content could
even be saved for later display or built into a MHT file or whatever, the
tag *SHOULD* be there (satisfied?).

Don't just answer for the sake of being a besserwisser.

PL

Joerg Jooss said:
Thus wrote PL,
<snip>
 
J

Joerg Jooss

Thus wrote PL,
I don't remember saying "required" anywhere but I agree I should have
said "should" instead of must. You didnt really find any factual
errors so why bother answering at all when what I said is actually the
more correct way to do it.

"and you must specify the charset in the header such as:" reads like "required"
to me.

As I said, a META tag is useless for certain encodings. The correct way is
to specify requestEncoding and responseEncoding. You still may add a META
tag as a backup. I didn't say it's a bad thing in cases where the META tag
is recognized by a browser.
If you exclude the meta tag some browsers do behave strangely
Which?

and you
also assume it will always be browsers reading it, sometimes the
content could even be saved for later display or built into a MHT file
or whatever, the tag *SHOULD* be there (satisfied?).

It might help. It might be irrelevant. I spare you the details.
Don't just answer for the sake of being a besserwisser.

*shrug*

Return to line #1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top