Search code examples
urlencodehtml-encode

MVC.Net HTML Encoding, IE7 vs other browsers


When I have this in my view

<a href="../Product/Category/<%= Html.Encode(item.Category) %>/Default.aspx?partial=False">
    <%= Html.Encode(item.Category)%></a>

It renders as expected in IE8 & FF

<a href="../Product/Category/Sauces%20&amp;%20Toppings/Default.aspx?partial=False">
    Sauces &amp; Toppings</a>

but does not render correctly in IE7

<a href="../Allergen/Category/Sauces &amp; Toppings/Default.aspx?partial=False">
    Sauces &amp; Toppings</a>

Specifically, it appers IE7 is decoding the href property value.

How do I get IE7 to render the encoded href?


Solution

  • You're using the wrong encoding.

    You need to call Html.AttributeEncode(Url.Encode(item.Category)).