HTML Encoding Strings - ASP.NET Web Forms VS Razor

2019-02-21 07:21发布

问题:

I'm not quite sure how this works yet... trying to find documentation.

In my existing app I've got two different ways of rendering strings in my View

<%: model.something %>
<!-- or -->
<%= model.something %>

The first one is html encoded, and the second one is not.

Is there something similarly short in Razor? All I can find is this, which is the encoded version.

@model.something

回答1:

I guess the best approach would be to use the Raw extension-method: @Html.Raw(Model.Something)



回答2:

@Model.Something automatically HTML encodes. If you want to avoid HTML encoding (and you want this only if you are absolutely sure what you are doing) you could use @MvcHtmlString.Create(Model.Something) (basically everything that implements IHtmlString won't be encoded). Phil Haack blogged about the Razor view engine syntax.