Why is MVC 4 Razor escaping ampersand when using HTML.Raw in a title attribute

蹲街弑〆低调 提交于 2019-12-18 03:32:12

问题


We recently upgraded to MVC 4 and now we are having titles in our links not display correctly. The problem is before HTML.Raw would not escape & in our title attributes, but now it does. Below is my sample code:

<a title="@Html.Raw("Shoe Size 6&#189;-8")">Test</a>

Which produces the following markup:

<a title="Shoe Size 6&amp;#189;-8">Test</a>

The only solution I found so far was to put the entire anchor into a string and then HTML.Raw that string.

Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?.

This is a very ugly solution and I am hoping there is a better alternative.


回答1:


While it is only a small step less ugly workaround, you can simply @Html.Raw the full attribute name and value.

<a @Html.Raw("title=\"Show Size 6&#189;-8\"")>Test</a>

Results in:

<a title="Show Size 6&#189;-8">Test</a>



回答2:


If you can't do the workaround listed above, I have a patched base-class you could try injecting via web.config. Check it out at https://gist.github.com/4036121



来源:https://stackoverflow.com/questions/12321616/why-is-mvc-4-razor-escaping-ampersand-when-using-html-raw-in-a-title-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!