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

This is happening because something in the pipeline (I'd guess razor but I'd need to look it up) attribute encodes all attribute values. This should not affect the browser from reaching your desired location however.

You can test this with the @Html.ActionLink(text, action, routeAttributes) overload.

@Html.ActionLink("Test", "Index", new { tony = "1", raul = 2 })

outputs

<a href="/?tony=1&amp;raul=2">Test</a>

In regards to your edit, you just need to make the entire <param> part of your raw value.

@{
    var test = "<param src=\"http://someurl.com/someimage.png?a=1234&b=5678\">";
}

<div>
    <object>
    @Html.Raw(test)
    </object>
</div>

just try below... It works perfect.

@{
    string test = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="[email protected];
}
<div>
    <a href="@Html.Raw(System.Web.HttpUtility.HtmlDecode(test))">@Html.Raw(System.Web.HttpUtility.HtmlDecode(test))</a>
</div>

Like this:

@{
    string test = "http://someurl.com/someimage.png?a=1234&b=5678";
}

<div>
    <a href="@Html.Raw(Html.AttributeEncode(test))">Test</a>
</div>

produces valid markup:

<div>
    <a href="http://someurl.com/someimage.png?a=1234&amp;b=5678">Test</a>
</div>

But this doesn't work. The ampersand gets encoded and the HTML is rendered as:

But that's exactly how a valid markup should look like. The ampersand must be encoded when used as an attribute. Don't worry, the browser will perfectly fine understand this url.

Notice that the following is invalid markup, so you don't want this:

<div>
    <a href="http://someurl.com/someimage.png?a=1234&b=5678">Test</a>
</div>

Try

<div>
    <a href="@MvcHtmlString.Create(test)">Test</a>
</div>