开发者

MVC3 - Display literal that parses as XML

This question feels ridiculous...I wrote literal text in Index.cshtml:

<div> blah blah blah <HereBeSomeText>. Blahdy friggen开发者_如何学JAVA blah.</div>

How do I escape or write out <HereBeSomeText> so that it is displayed as content on the page rather than interpreted as bad HTML?


Just use the common entities for < and >:

<div> blah blah blah &lt;HereBeSomeText&gt;. Blahdy friggen blah.</div>


While I'm not a Razor expert, it seems like something like this ought to work:

<div>
    @"blah blah blah <HereBeSomeText>. Blahdy friggen blah."
</div>

or

<div>
    @new HtmlString(
        Html.Encode("blah blah blah <HereBeSomeText>. Blahdy friggen blah."))
</div>

The actual HTML produced would be something like:

<div>
    blah blah blah &lt;HereBeSomeText&gt;. Blahdy friggen blah.
</div>

... in which the browser would convert < and > to < and > respectively.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜