3
Vote

Html.Raw used as attribute value in Razor is still being encoded

description

Steps:
  • Load an MVC 4 project.
  • Go into a view (e.g., ~/Views/Shared/_Layout.cshtml).
  • Add an attribute to an HTML element and make its value something that is already attribute-encoded and wrapped in @Html.Raw.
  • Run site and find the title attribute is encoded, despite being run through @Html.Raw.
Example:
<a title="@Html.Raw("Show Size 6&#189;-8")">Test1</a>
Expected:
<a title="Show Size 6&#189;-8">Test1</a>
Actual (doubly-encoded somehow):
<a title="Show Size 6&amp;#189;-8">Test1</a>
[Original Stack Overflow question: http://stackoverflow.com/q/12321616/48700]

comments

anurse wrote Nov 8, 2012 at 1:19 AM

Yep, there's a bug here. I've got patched WebViewPage base classes here: https://gist.github.com/4036121 which can help work-around the problem. We'll put it on our queue for the next release!