开发者

ASP.NET MVC how to display html tags as html?

开发者 https://www.devze.com 2023-01-27 22:20 出处:网络
I display some text in the view: .... <%: model.Content %> .... my model.Content contains html tags, and I want to display开发者_StackOverflow社区 them not as text, but as html. How to do it

I display some text in the view:

....
 <%: model.Content %>
....

my model.Content contains html tags, and I want to display开发者_StackOverflow社区 them not as text, but as html. How to do it?

Thanks.


As of MVC 3 you can use:

@Html.Raw(model.Content)


<%= model.Content %>

Be careful with this because it could open your site to XSS attacks.


Use:

<%: MvcHtmlString.Create(model.Content) %>

or

<%= model.Content %>

Because <%: does Html encoding, while <%= doesn't.

MvcHtmlString.Create creates a 'save' Html string, which<%: takes and prints out as is.


<%= Model.Content %>

The colon : is short for Html.Encode() while equal = simply post what is in the string.

0

精彩评论

暂无评论...
验证码 换一张
取 消