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 <HereBeSomeText>. 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 <HereBeSomeText>. Blahdy friggen blah.
</div>
... in which the browser would convert < and > to <
and >
respectively.
精彩评论