Search code examples
asp.net-mvc-3html-encode

Why is my custom HTML Helper result getting html encoded?


I've got the following custom html helper in asp.net mvc 3

public static string RegisterJS(this HtmlHelper helper, ScriptLibrary scriptLib)
{
   return "<script type=\"text/javascript\"></script>\r\n";
}

The problem is that the result is getting html encoded like so (I had to add spaces to get so to show the result properly:

   &lt;script type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

This obviously isn't much help to me.. Nothing I've read says anything about this.. any thoughts on how I can get my real result back?


Solution

  • You're calling the helper in a Razor @ block or an ASPX <%: %> block.
    These constructs automatically escape their output.

    You need to change the helper to return an HtmlString, which will not be escaped:

    return new HtmlString("<script ...");