@ Html.Raw坚持编码报价 [英] @Html.Raw insists on encoding quotes

查看:128
本文介绍了@ Html.Raw坚持编码报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC4 的Razor视图。鉴于字符串和backgroundImage 设置是这样的:

MVC4 razor view. Given that a string backgroundImage is set like this:

backgroundImage = string.Format("background: url('{0}') top left no-repeat;", project.MainImage);

为什么这

<div class="spotlight slide teaser-text" id="@slideId" style="@Html.Raw(backgroundImage)">

农产品

<div class="spotlight slide teaser-text" id="spotlight-0" style="background: url(&#39;/media/215/spotlight01.jpg&#39;) top left no-repeat;">

Html.Raw 新MvcHtmlString MvcHtmlString.Create 所有的行为类似。

我会希望它制作

<div class="spotlight slide teaser-text" id="spotlight-0" style="background: url('/media/215/spotlight01.jpg') top left no-repeat;">

(注意引号)。

推荐答案

我有看对含有该标记剃刀模板编译的DLL。据我所看到的,问题是这样的。

I have had a look at the compiled DLL for a razor template containing this markup. As far as I can see, the issue is this.

Html.Raw 把一个字符串转换成 HtmlString HttpUtility.HtmlEn code 治疗 IHtmlString 情况不同,以普通字符串,因为它并不适用于HTML编码

Html.Raw turns a string into an HtmlString. HttpUtility.HtmlEncode treats IHtmlString instances differently to normal strings, in that it doesn't apply Html encoding.

当的编程定义的字符串的属性的上下文中,给出一个呼叫到由

When the programmatically defined string is given in the context of the attribute, a call is made to

this.WriteAttribute(...)

用适当的生成属性数据。这一数据的一部分是 Html.Raw 由呼叫产生的HtmlString。另一部分是一个标志,指示该值不是字面

with data appropriate to generate the attribute. Part of this data is the HtmlString generated by the call to Html.Raw. Another part is a flag indicating that the value is not a literal.

由于没有文字的标记状态时, WriteAttribute 方法最终调用 HtmlEn code 。但是,调用此的不可以 HtmlString ,但在字符串值此对象。这样你就结束了一个HTML连接输出codeD值。

Because of the not-literal flag status, the WriteAttribute method eventually calls HtmlEncode. But it calls this not on the HtmlString, but on the string value of this object. So you do end up with an HTML encoded value in the output.

的属性的上下文之外,该呼叫到由

Outside the context of an attribute, the call is made to

this.Write(...)

和这不来自相同问题的困扰。所以好像我(至少如果我通过精确跟踪这一切)。

and this doesn't suffer from the same problem. So it seems like a bug to me (at least if I've accurately traced it all through).

这篇关于@ Html.Raw坚持编码报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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