Emitting unencoded strings in a Razor view

Viewed 26712

As ScottGu says in his blog post «by default content emitted using a @ block is automatically HTML encoded to better protect against XSS attack scenarios». My question is: how can you output a non-HTML-encoded string?

For the sake of simplicity, pls stick to this simple case:

@{
 var html = "<a href='#'>Click me</a>"
 // I want to emit the previous string as pure HTML code...
}
5 Answers

I'm using ASP.NET MVC and Razor under Mono.

I couldn't get HtmlHelper from System.Web.WebPages of System.Web.Mvc for some reasons.

But I managed to output unencoded string after declaring model's property as RazorEngine.Text.RawString. Now it outputs as expected.

Example

@{
    var txt = new RawString("some text with \"quotes\"");
    var txt2 = "some text with \"quotes\"";
}
<div>Here is unencoded text: @txt</div>
<div>Here is encoded text: @txt2</div>

Output:

<div>Here is unencoded text: some text with "quotes"</div>
<div>Here is encoded text: some text with &quot;quotes&quot;</div>
Related