禁用的UNI code字符编码在ASP.NET的MVC3 [英] Disable encoding of unicode characters in ASP.NET-MVC3

查看:89
本文介绍了禁用的UNI code字符编码在ASP.NET的MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站每天的文本担任UTF-8。

On my site every text is served as UTF-8.

由于现今所有浏览器都支持UNI code字,我想使用它们原样。

Since nowadays every browser supports unicode characters, I would like to use them as-is.

在asp.net框架是用数字字符引用替换任何单code,如&放大器非常有帮助;#225; 。仅供参考检查: http://en.wikipedia.org/wiki/Uni$c $#c_and_HTML HTML_document_characters

The asp.net framework is very helpful by replacing any unicode with a Numeric Character Reference, like á. For reference check: http://en.wikipedia.org/wiki/Unicode_and_HTML#HTML_document_characters

当然,这样的网页正确呈现在最古老的Netscape的可能,但例如谷歌分析电子商务模块有一些麻烦了解这些特殊codeD字符。

Sure, this way the webpage renders correctly in the oldest netscape possible, but for example the google analytics ecommerce module has some trouble understanding these specially coded characters.

有没有办法关闭全局的数字字符引用编码?

Is there a way to globally disable the Numeric Character Reference encoding?

例如我想在剃刀写:

<span class="title">@ViewBag.Title</span>

我想这显示在输出:

I would want this to show on the output:

<span class="title">Számítástechnika</span>

不是这个:

<span class="title">Sz&#225;m&#237;t&#225;stechnika</span>

我并不想禁用HTML编码,因此Html.Raw是不是一个解决方案,例如我不能保证@ ViewBag.Title不会的内容是这样的:

I'm not trying to disable the html encoding, so Html.Raw is not a solution, as for example I'm not able to ensure that the @ViewBag.Title will not content something like this:

<span class="title"><script>alert('injected hahahah');</script></span>

所以我含量的特殊的HTML字符自动编码。这不是我想要禁用的。

So I'm content with the automatic encoding of special html characters. That is not what I want to disable.

我不希望重组所有code,我认为应该有一个全球性开关禁止这种行为在剃刀用字符串参数。有没有办法做到这一点?

I wouldn't want to restructure all the code, and I thought that there should be a "global switch" to disable this kind of behavior in using string parameters in razor. Is there a way to do this?

也可以我明确禁止的数字字符引用,例如用类似新MvcHtmlString(MyString的,有些参数)

Also can I explicitly forbid the numeric character references, for example with something like new MvcHtmlString(myString, some parameters) ?

推荐答案

我怕你不能把这种编码功能关闭。这个好的功能是由 WebUtility.HtmlEn code 你不能影响编码。

I'm afraid that you cannot turn this encoding feature off. This "nice" feature is provided by the WebUtility.HtmlEncode and you cannot influence the encoding.

然而,随着开始的 .NET 4.0 您可以自定义编码行为,与创建从继承的类的 HttpEn codeR 和配置在web.cofig <一个href=\"http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.en$c$crtype.aspx\"相对=nofollow> HttpRuntimeSection.En coderType 。但是,你需要实现自己的自定义编码逻辑。

However with starting .net 4.0 you can customize the encoding behavior, with creating a class that inherits from the HttpEncoder and configure it in the web.cofig HttpRuntimeSection.EncoderType. But you need to implement your own custom encoding logic.

幸运的 .NET 4.5 附带了一个新的 HttpEn codeR 其中EN codeS坏的东西(如&LT;脚本&GT; ),但是处理的Uni code字符正确调用的 AntiXssEn codeR

Luckily .net 4.5 ships with a new HttpEncoder which encodes the bad stuff (like <script>) however handles the Unicode characters correctly called AntiXssEncoder

所以,你只需要在你的web.config补充一点:

So you just need to add this in your web.config:

<system.web>
    <httpRuntime encoderType="System.Web.Security.AntiXss.AntiXssEncoder, 
                             System.Web, Version=4.0.0.0, Culture=neutral, 
                             PublicKeyToken=b03f5f7f11d50a3a"/>    
</system.web>

如果您还不是在.NET 4.5就可以的帮助下实现你的 AntiXssEn codeR
的Microsoft Web防护库

If you are not yet on .net 4.5 you can implement your AntiXssEncoder with the help of Microsoft Web Protection Library

下面是一篇文章如何设置它:的使用AntiXss作为默认的连接codeR对于ASP.NET (尽管它可能是过时的)

Here is an article how to set it up: Using AntiXss As The Default Encoder For ASP.NET (although it might be outdated)

这篇关于禁用的UNI code字符编码在ASP.NET的MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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