P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

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 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?

Upvotes: 2

Views: 313

Answers (2)

Lucero
Lucero

Reputation: 60190

Just use the common entities for < and >:

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

Upvotes: 1

StriplingWarrior
StriplingWarrior

Reputation: 156524

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.

Upvotes: 0

Related Questions