title: Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?
body: <p>I have a URL I would like to render in an anchor tag as-is in a Razor view. I would have thought Html.Raw would be the way to go:</p>
  
  <pre><code>@{
      string test = "http://someurl.com/someimage.png?a=1234&amp;b=5678";
  }
  
  &lt;div&gt;
      &lt;a href="@Html.Raw(test)"&gt;Test&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But this doesn't work. The ampersand gets encoded and the HTML is rendered as:</p>
  
  <pre><code>&lt;div&gt;
      &lt;a href="http://someurl.com/someimage.png?a=1234&amp;amp;b=5678"&gt;Test&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The strange thing is that if I do the Html.Raw call outside of the anchor tag, the HTML output is as expected:</p>
  
  <pre><code>&lt;div&gt;
      @Html.Raw(test)
  &lt;div&gt;
  </code></pre>
  
  <p>is rendered as:</p>
  
  <pre><code>&lt;div&gt;
      http://someurl.com/someimage.png?a=1234&amp;b=5678
  &lt;/div&gt;
  </code></pre>
  
  <p>Can anyone explain why this is?</p>
  
  <p>Edit:</p>
  
  <p>Tried a few other things out, and found that the following works as expected:</p>
  
  <pre><code>&lt;div data-url="@Html.Raw(test)"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>&lt;div data-url="http://someurl.com/someimage.png?a=1234&amp;b=5678"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>To add a little more context, <strong>my goal is not actually to use the URL in an anchor tag</strong>, since <code>href</code>s can be HTML encoded and still work (I just used the anchor tag case as an example). Rather I wish to use the URL in an <code>&lt;object&gt; &lt;param&gt;</code> value tag for a Flash object. The Flash object doesn't properly recognize the HTML encoded URL, but I can't get the raw URL to output correctly.</p>
  
  <p>I am at a loss. Time to break out the MVC source code I guess...</p>