Attempting to make my first ASP.NET page. Got IIS 5.1 on XP, configured to run .NET 4. Created a new virtual directory and added an .aspx file. When I browse the file, non-ASCII characters are corrupted. For instance, an ü (U+00FC) is transformed to ü (U+00C3 U+00BC), which is the I-don't-get-this-is-UTF-8 equivalent.
I have tried various ways of availing this:
- I made sure the .aspx file is indeed encoded as UTF-8.
I set the meta tag:
<meta charset="UTF-8">
I set the virtual directory to handle .aspx as
text/html;charset=utf-8
under HTTP Headers > File Type in IIS.- I added
ResponseEncoding="utf-8"
to<%@ Page ... %>
. - I inserted the string in
HttpUtility.HtmlEncoded()
. Now the ü was transformed to ü (U+00C3 U+00BC).
Finally, I found 2 ways that worked:
- Replacing non-ASCII characters with character references, such as
ü
This was okay in the 90's, not today. Adding a web.config file to the virtual directory, with this content:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <globalization fileEncoding="utf-8"/> </system.web> </configuration>
Without fileEncoding
setting, the ASP.NET parser will read the .aspx and corrupt every non-ASCII character without attempting to infer the file encoding. Is this just something you pros have learned to live with, or am I missing something? Is a web.config file with globalization settings the way to handle "international" characters on .aspx pages? I don't remember having similar problems with PHP, so I'm puzzled why this crops up with ASP.NET.
To use non-ASCII characters you need to have two things. Save the files using UTF-8, by choosing this encoding for the files and be sure that you have these settings on your web.config
Note that there is always a web.config on ASP.NET. There is the global one that also has these settings and lives in the asp.net directory
{drive:}\WINDOWS\Microsoft.NET\Framework\{version}\CONFIG\
, and then the web.config on your project. Sometimes the global one sets the encoding from the current country. In this case you need to set it back to UTF-8 in your project.You have found all that already, I just point out the 3 settings: