首页 > 解决方案 > ASP.NET MVC - 渲染重音字符

问题描述

在我看来,我在渲染法语、西班牙语等重音字符时遇到问题。

例如。而不是渲染Olá它呈现为Olá

我确实尝试过@Html.Raw(myVariable)。它确实奏效了。但是,在我的应用程序中使用它非常烦人。有没有办法或解决方案可以在我的应用程序中进行这项工作?

我试过这个

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

但这没有用。

标签: asp.net-mvcrazorasp.net-mvc-5html-encode

解决方案


To prevent the need for using Html.Raw(), you have these options:

  1. Avoid storing HTML encoded text in your database;

  2. Avoid putting HTML encoded text in variables/properties of type string;

  3. If all you have is HTML encoded text, then you can use type MvcHtmlString for your variables/properties, and then Razor will refrain from encoding:

    var myEncodedVariable = MvcHtmlString.Create(myVariable);
    

    and then in Razor:

    @myEncodedVariable
    

    although I agree this is little more than using a different workaround at a different location.


推荐阅读