Search code examples
htmlasp.net-mvcasp.net-mvc-2asp.net-mvc-3viewengine

How would I tell a browser to render a string, not display it


I have this extension method I created and it returns a string of all the categories in my database as hyperlinks. Great!

@Html.MyMenu()

The problem is that the links are being displayed as text and not rendered as hyperlinks.

When viewing the source code I see:

<div id="menucontainer">

                &lt;a href=&quot;/Anuncio/Electronics&quot;&gt;Electronics&lt;/a&gt;&lt;a href=&quot;/Anuncio/Clothes&quot;&gt;Clothes&lt;/a&gt;&lt;a href=&quot;/Anuncio/Domestic&quot;&gt;Domestic&lt;/a&gt;&lt;a href=&quot;/Anuncio/Garden&quot;&gt;Garden&lt;/a&gt;

            </div>

I think I may be wrong but I remember that in MVC2 (using the default view engine) you had:

<%: this is rendered, right? %>

Or am I mistaken? Anyways, I'm using MVC3 and the Razor engine. Thanks a lot for your help guys. I'm really enjoying learning as much as I can about this.


Solution

  • Razor escapes HTML by default.

    To avoid that, please do something like this:

    Writing/outputting HTML strings unescaped