HTML Encoding Strings - ASP.NET Web Forms VS Razor View Engine

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 13:41:16

问题


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.



来源:https://stackoverflow.com/questions/4173483/html-encoding-strings-asp-net-web-forms-vs-razor-view-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!