我需要web.config的非ASCII字符吗? [英] Do I need web.config for non-ASCII characters?

查看:106
本文介绍了我需要web.config的非ASCII字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试制作我的第一个ASP.NET页面。在XP上获得IIS 5.1,配置为运行.NET 4.创建一个新的虚拟目录并添加了一个.aspx文件。浏览文件时,非ASCII字符已损坏。例如,ü(U + 00FC)被转换为¼(U + 00C3 U + 00BC),这是I-don't-get-this-is-UTF-8等价物。



我尝试了各种各样的方法:


  1. 我确定.aspx文件的确是编码为UTF-8。

  2. 我设置了元标记:



    < meta charset = UTF-8>


  3. 我设置虚拟目录来处理.aspx为


  4. 我添加了 ResponseEncoding =utf -8<%@ Page ...%>

  5. 字符串 HttpUtility.HtmlEncoded()。现在ü被转换为¼(U + 00C3 U + 00BC)。

最后,我发现有两种方法可行: p>


  1. 使用字符引用替换非ASCII字符,例如&#252; 可以在90年代,而不是今天。

  2. 将web.config文件添加到虚拟目录,其中包含以下内容:

     <?xml version =1.0encoding =utf-8?> 
    < configuration>
    < system.web>
    < worldwide fileEncoding =utf-8/>
    < /system.web>
    < / configuration>


没有 fileEncoding 设置,ASP.NET解析器将读取.aspx并损坏每个非ASCII字符,而不尝试推断文件编码。这是你的职业生涯中学到的东西,还是我错过了什么?是一个具有全球化设置的web.config文件 方式来处理.aspx页面上的国际字符?我不记得有与PHP类似的问题,所以我很困惑为什么这会与ASP.NET一起起作用。

解决方案

要使用非ASCII字符,您需要有两件事情。使用UTF-8保存文件,通过为文件选择此编码,并确保您在 web.config

中有这些设置

 < worldwide requestEncoding =utf-8responseEncoding =utf-8fileEncoding =utf-8/> 

请注意,ASP.NET上总是有一个web.config。有一个全局的,也有这些设置,并且生活在asp.net目录 {驱动器:} \WINDOWS\Microsoft.NET\Framework\ {版本} \CONFIG\ ,然后在您的项目上的web.config。有时候,全局设置来自当前国家的编码。在这种情况下,您需要在项目中将其设置为UTF-8。



您已经找到所有这些,我只是指出3个设置:



  1. 使用unicode保存文件。

  2. 设置requestEncoding =utf-8

  3. 设置responseEncoding =utf-8


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.

解决方案

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"

这篇关于我需要web.config的非ASCII字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆