Writing HTML code inside variable in ASP.NET C# and Razor

The Razor engine HTML encodes strings by default, as you have noticed. To avoid this behavior, just use Html.Raw():

<ul>@Html.Raw(List)</ul>

Edit

To render a variable within a string, I suppose you could use string.Format:

@{ var someVariable = "world"; }
@string.Format("<div>hello {0}</div>", someVariable)

Although that seems like overkill (at least for this example) when you can just write:

<div>hello @someVariable</div>

Leave a Comment

tech