如何为我的 MVC 应用程序允许 web.config 中的 unicode 字符?

How to allow unicode characters in web.config for my MVC application?

我想在名字中添加一些很酷的字符,并在我的 web.config 中添加了一张牛的图片,就像这样。

<add key="SenderName" value="&#x1f3eb; Mr Mooo"/>

但是,当我启动该应用程序时,我在 _Layout.cshtml 唠叨本地化等问题的这一行出现异常。

@Styles.Render("~/Content/css")

我不知道该如何处理。显然,MVC 讨厌奶牛,但我能做些什么呢?

我尝试遵循配置文件的 the suggested syntax according to the standard. I also added UTF-16 at the top

尝试在 header 和 system.web 的配置节点中设置编码- 将全球化节点 "fileEncoding" 属性设置为 utf-16。

<?xml version="1.0" encoding="utf-16"?>
<configuration>
  <system.web>
    <globalization fileEncoding="utf-16" requestEncoding="utf-16" responseEncoding="utf-16"/>
  </system.web>
</configuration>

P.S。抱歉 - 上次我是在平板电脑上打字,所以很难写一个例子。

这与 Unicode 字符完全无关,而与 XML 转义有关。

如您链接到的 XML Standard 中所述,XML 仅定义了 5 个标准实体:

  • &lt;表示<
  • &gt;表示>
  • &amp;表示&
  • &apos;表示'
  • &quot;表示"

所有其他实体(例如 &#x1f3eb;)在 XML 中无效,除非您明确定义它们。

不过,有一种简单的方法可以摆脱这种情况。这个实体的问题是它包含一个未转义的符号,这使得它与 XML 不兼容。你只需要逃避它,世界就一切都好。

<add key="SenderName" value="&amp;#x1f3eb; Mr Mooo"/>

现在您的 应用程序 将收到您想要的未转义字符串:&#x1f3eb; Mr Mooo 当您从配置文件中检索它时,然后您可以将它传递给视图.