如何使ASP.NET MVC视图正确输出unicode字符? [英] How can I make ASP.NET MVC views output unicode characters correctly?
问题描述
当我使用<%= ...%>语法并传入包含非ASCII字符的字符串时,该字符串将被解码并输出为ASCII.例如:<%=é"%>输出é.
When I use the <%=...%> syntax and pass in a string with non-ascii characters, the string is decoded and outputed as ASCII. For example: <%="é"%> outputs é.
如何让视图输出编码的字符呢?
How can I make the views output the encoded characters instead?
推荐答案
为什么要HTML编码?如果您的页面是utf-8,应该没有问题.但是,如果要进行HTML编码,则可以在ASP.NET 4.0中使用<%:
:
Why do you want to HTML encode? If your page is utf-8 there should be no problem. But if you want to HTML encode you could use <%:
in ASP.NET 4.0:
<%: "é" %>
和:
<%= HttpUtility.HtmlEncode("é") %>
在旧版本中.
我个人建议您在Web的< globalization>
元素中将 requestEncoding
和 responseEncoding
设置为utf-8.配置并将< meta charset ="utf-8"/>
(HTML5)标记放在母版页的首页部分,就可以了.
Personally I would recommend you to set the requestEncoding
and responseEncoding
to utf-8 in the <globalization>
element in your web.config and put a <meta charset="utf-8" />
(HTML5) tag in the head section of your master page and you should be fine.
这篇关于如何使ASP.NET MVC视图正确输出unicode字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!