Do I need web.config for non-ASCII characters?

2019-05-03 09:11发布

问题:

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:

  1. I made sure the .aspx file is indeed encoded as UTF-8.
  2. I set the meta tag:

    <meta charset="UTF-8">

  3. I set the virtual directory to handle .aspx as text/html;charset=utf-8 under HTTP Headers > File Type in IIS.

  4. I added ResponseEncoding="utf-8" to <%@ Page ... %>.
  5. I inserted the string in HttpUtility.HtmlEncoded(). Now the ü was transformed to ü (U+00C3 U+00BC).

Finally, I found 2 ways that worked:

  1. Replacing non-ASCII characters with character references, such as &#252; This was okay in the 90's, not today.
  2. 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.

回答1:

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

<globalization requestEncoding="utf-8" responseEncoding="utf-8"  fileEncoding="utf-8" />

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:

  1. Save your files with unicode.
  2. Set the requestEncoding="utf-8"
  3. Set the responseEncoding="utf-8"