HTML in MVC RouteLink

asp.net-mvc, asp.net-mvc-3

Solution

If you want HTML inside your anchor don't use the `Html.RouteLink` (because it will HTML encode the link text by default as you noticed) instead of build your `a` tag by hand with using `Url.RouteUrl` to generate the url:

<p class="articleLink">
    <a href="@(Url.RouteUrl("Article_Route",
                   new RouteValueDictionary() 
                      { { "articleId", article.Id }, 
                        { "seoUrl", article.SeoUrl } }))">
        @Html.Raw(article.Title)
    </a>
</p>

Or you can create your own non encoding `RouteLink` helper.

Problem

I have a RouteLink constructed like so ``` <p class="articleLink"> @MvcHelper.Html.RouteLink(article.Title, "Article_Route", new RouteValueDictionary() { { "articleId", article.Id }, { "seoUrl", article.SeoUrl } })) </p> ``` However, `article.Title` could potentially contain HTML i.e. the value could be `<em>Sample</em> Title` which in turn gets rendered like so ``` <a href="/Article/111111/Sample-Title">&lt;em&gt;Sample&lt;/em&gt; Title</a> ``` Is there any way to prevent the HTML from being escaped, and instead to be treated as actual HTML? Or do I need to create a standard HTML `<a href...` link in this case (thus losing all the niceties associated with the RouteLink helper).

Original source