始终使用MVC3和Razor输出原始HTML

时间:2021-09-26 22:28:33

I’ve got a class with a property that looks like this:

我有一个类似于以下属性的类:

[AllowHtml]
[DataType(DataType.MultilineText)]
public string Description { get; set; }

I’ve already put in the [AllowHtml] attribute to let me submit HTML to this property via the form that I’ve built, but what I want to do is output the value of the property as the raw HTML without it being escaped.

我已经放入[AllowHtml]属性让我通过我构建的表单向这个属性提交HTML,但我想要做的是输出属性的值作为原始HTML而不进行转义。

I know I can use Html.Raw(Model.Description) but what I’m looking for is some way of telling Html.DisplayFor(m => m.Description) to always output the raw HTML. Is there an attribute I can use to decorate the properties in my class that I wish to behave like that?

我知道我可以使用Html.Raw(Model.Description),但我正在寻找的是告诉Html.DisplayFor(m => m.Description)始终输出原始HTML的一些方法。是否有一个属性我可以用来装饰我班级的属性,我希望这样做?

Basically it’s me being lazy—I don’t want to have to remember which properties might contain HTML, so I don’t want to have to think about using Html.Raw(…) when I need to do the above—I’d much rather my Model know what it should do and do it automatically. I’ve tried searching for an answer, but either I’m not phrasing it correctly or there’s no way of doing it :(

基本上我是懒惰的 - 我不想记住哪些属性可能包含HTML,所以我不想在我需要做上面的时候考虑使用Html.Raw(...) - 我更确切地说,我的模型知道它应该做什么并自动完成。我试过寻找答案,但要么我没有正确地说它或者没有办法做到这一点:(

Thanks,

谢谢,

3 个解决方案

#1


20  

Change your Description proerpty to return an HtmlString.

更改Description proerpty以返回HtmlString。

Razor does not escape HtmlString values.
(In fact, all Html.Raw does is create an HtmlString)

Razor不会逃避HtmlString值。 (事实上​​,所有Html.Raw都会创建一个HtmlString)

#2


5  

This is actually rather simple (once you know how...). Change your DataType attrib to [DataType(DataType.Html)], and create a partial view, put it in Views/Shared/DisplayTemplates/Html.cshtml, with this:

这实际上相当简单(一旦你知道如何......)。将您的DataType属性更改为[DataType(DataType.Html)],并创建一个局部视图,将其放在Views / Shared / DisplayTemplates / Html.cshtml中,使用:

@model string
@Html.Raw(Model)

Of course you can also not change your DataType attrib, and name the view MultilineText.cshtml instead of Html.cshtml.

当然,您也不能更改DataType attrib,并将视图命名为MultilineText.cshtml而不是Html.cshtml。

#3


2  

Just to provide a bit more info here - your issue is that @ will always HtmlEncode unless you have IHtmlString returned - so the issue is sourced at the @ symbol. It's one of the benefits of the razor syntax - its safer to htmlencode than to not. So there is no 'quick' way here since the root of your issue is the @ symbol which will exclude HtmlEncoding if it finds IHtmlString. So - no 'quick' way around this unless you use the old <% syntax which IMHO sucks compared to razor : )

只是在这里提供更多信息 - 你的问题是@将始终是HtmlEncode,除非你返回了IHtmlString - 所以问题来自@符号。这是剃刀语法的好处之一 - 它对htmlencode更安全。所以这里没有“快速”的方法,因为你的问题的根源是@符号,如果找到IHtmlString,它将排除HtmlEncoding。所以 - 没有“快速”解决这个问题,除非你使用与剃刀相比恕我前悔的旧的<%语法:)

#1


20  

Change your Description proerpty to return an HtmlString.

更改Description proerpty以返回HtmlString。

Razor does not escape HtmlString values.
(In fact, all Html.Raw does is create an HtmlString)

Razor不会逃避HtmlString值。 (事实上​​,所有Html.Raw都会创建一个HtmlString)

#2


5  

This is actually rather simple (once you know how...). Change your DataType attrib to [DataType(DataType.Html)], and create a partial view, put it in Views/Shared/DisplayTemplates/Html.cshtml, with this:

这实际上相当简单(一旦你知道如何......)。将您的DataType属性更改为[DataType(DataType.Html)],并创建一个局部视图,将其放在Views / Shared / DisplayTemplates / Html.cshtml中,使用:

@model string
@Html.Raw(Model)

Of course you can also not change your DataType attrib, and name the view MultilineText.cshtml instead of Html.cshtml.

当然,您也不能更改DataType attrib,并将视图命名为MultilineText.cshtml而不是Html.cshtml。

#3


2  

Just to provide a bit more info here - your issue is that @ will always HtmlEncode unless you have IHtmlString returned - so the issue is sourced at the @ symbol. It's one of the benefits of the razor syntax - its safer to htmlencode than to not. So there is no 'quick' way here since the root of your issue is the @ symbol which will exclude HtmlEncoding if it finds IHtmlString. So - no 'quick' way around this unless you use the old <% syntax which IMHO sucks compared to razor : )

只是在这里提供更多信息 - 你的问题是@将始终是HtmlEncode,除非你返回了IHtmlString - 所以问题来自@符号。这是剃刀语法的好处之一 - 它对htmlencode更安全。所以这里没有“快速”的方法,因为你的问题的根源是@符号,如果找到IHtmlString,它将排除HtmlEncoding。所以 - 没有“快速”解决这个问题,除非你使用与剃刀相比恕我前悔的旧的<%语法:)