简体   繁体   English

什么是MvcHtmlString,我应该何时使用它?

[英]What is an MvcHtmlString and when should I use it?

The documentation for MvcHtmlString is not terribly enlightening: MvcHtmlString文档并不十分具有启发性:

Represents an HTML-encoded string that should not be encoded again. 表示不应再次编码的HTML编码字符串。

It's not clear to me what exactly the implications of this are. 我不清楚这究竟是什么意思。 It seems that some HTML helper methods return an MvcHtmlString , but several examples I've seen online of custom helpers just return a regular string. 似乎一些HTML帮助器方法返回一个MvcHtmlString ,但我在网上看到的自定义帮助器的几个例子只返回一个常规字符串。

Questions: 问题:

What is an MvcHtmlString ? 什么是MvcHtmlString

When should I choose MvcHtmlString over string and vice versa? 我何时应该选择MvcHtmlString不是string ,反之亦然? Why? 为什么?

ASP.NET 4 introduces a new code nugget syntax <%: %> . ASP.NET 4引入了一个新的代码块语法<%: %> Essentially, <%: foo %> translates to <%= HttpUtility.HtmlEncode(foo) %> . 基本上, <%: foo %>转换为<%= HttpUtility.HtmlEncode(foo) %> The team is trying to get developers to use <%: %> instead of <%= %> wherever possible to prevent XSS. 该团队正试图让开发人员尽可能使用<%: %>而不是<%= %>来阻止XSS。

However, this introduces the problem that if a code nugget already encodes its result, the <%: %> syntax will re-encode it. 但是,这会引入一个问题,即如果代码块已经对其结果进行了编码,则<%: %>语法将对其进行重新编码 This is solved by the introduction of the IHtmlString interface (new in .NET 4). 这是通过引入IHtmlString接口(.NET 4中的新增功能)来解决的。 If the foo() in <%: foo() %> returns an IHtmlString, the <%: %> syntax will not re-encode it. 如果把foo()<%: foo() %>返回IHtmlString,所述<%: %>语法将不会重新编码。

MVC 2's helpers return MvcHtmlString, which on ASP.NET 4 implements the interface IHtmlString. MVC 2的助手返回MvcHtmlString,它在ASP.NET 4上实现了接口IHtmlString。 Therefore when developers use <%: Html.*() %> in ASP.NET 4, the result won't be double-encoded. 因此,当开发人员在ASP.NET 4中使用<%: Html.*() %>时,结果将不会被双重编码。

Edit: 编辑:

An immediate benefit of this new syntax is that your views are a little cleaner. 这种新语法的直接好处是您的视图更清晰一些。 For example, you can write <%: ViewData["anything"] %> instead of <%= Html.Encode(ViewData["anything"]) %> . 例如,您可以编写<%: ViewData["anything"] %>而不是<%= Html.Encode(ViewData["anything"]) %>

This is a late answer but if anyone reading this question is using razor, what you should remember is that razor encodes everything by default, but by using MvcHtmlString in your html helpers you can tell razor that it doesn't need to encode it . 这是一个迟到的答案,但如果有人读这个问题是使用剃须刀,你应该记住的是剃刀默认编码一切, 但通过在你的html助手中使用MvcHtmlString你可以告诉剃刀它不需要编码它

If you want razor to not encode a string use 如果你想让razor不编码字符串使用

@Html.Raw("<span>hi</span>")

Decompiling Raw(), shows us that it's wrapping the string in a HtmlString 反编译Raw(),向我们显示它将字符串包装在HtmlString中

public IHtmlString Raw(string value) {
    return new HtmlString(value); 
}

" HtmlString only exists in ASP.NET 4. HtmlString仅存在于ASP.NET 4中。

MvcHtmlString was a compatibility shim added to MVC 2 to support both .NET 3.5 and .NET 4. Now that MVC 3 is .NET 4 only, it's a fairly trivial subclass of HtmlString presumably for MVC 2->3 for source compatibility. MvcHtmlString是一个兼容填充程序,添加到MVC 2以支持.NET 3.5和.NET 4.现在MVC 3只是.NET 4,它是HtmlString的一个相当简单的子类,可能是MVC 2-> 3的源兼容性。 " source 来源

A nice practical use of this is if you want to make your own HtmlHelper extensions. 如果你想制作自己的HtmlHelper扩展,那么这个实用的很好用。 For example, I hate trying to remember the <link> tag syntax, so I've created my own extension method to make a <link> tag: 例如,我讨厌尝试记住<link>标记语法,因此我创建了自己的扩展方法来生成<link>标记:

<Extension()> _
Public Function CssBlock(ByVal html As HtmlHelper, ByVal src As String, ByVal Optional ByVal htmlAttributes As Object = Nothing) As MvcHtmlString
    Dim tag = New TagBuilder("link")
    tag.MergeAttribute("type", "text/css")
    tag.MergeAttribute("rel", "stylesheet")
    tag.MergeAttribute("href", src)
    tag.MergeAttributes(New RouteValueDictionary(htmlAttributes))
    Dim result = tag.ToString(TagRenderMode.Normal)
    Return MvcHtmlString.Create(result)
End Function

I could have returned String from this method, but if I had the following would break: 我可以从这个方法返回String ,但如果我有以下会破坏:

<%: Html.CssBlock(Url.Content("~/sytles/mysite.css")) %>

With MvcHtmlString , using either <%: ... %> or <%= ... %> will both work correctly. 使用MvcHtmlString ,使用<%: ... %><%= ... %>都可以正常工作。

如果要将原始HTML传递给MVC帮助器方法并且不希望辅助方法对HTML进行编码,则可以使用MvcHtmlString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM