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:
@{
string test = "http://someurl.com/someimage.png?a=1234&b=5678";
}
<div>
<a href="@Html.Raw(test)">Test</a>
</div>
But this doesn't work. The ampersand gets encoded and the HTML is rendered as:
<div>
<a href="http://someurl.com/someimage.png?a=1234&b=5678">Test</a>
</div>
The strange thing is that if I do the Html.Raw call outside of the anchor tag, the HTML output is as expected:
<div>
@Html.Raw(test)
<div>
is rendered as:
<div>
http://someurl.com/someimage.png?a=1234&b=5678
</div>
Can anyone explain why this is?
Edit:
Tried a few other things out, and found that the following works as expected:
<div data-url="@Html.Raw(test)"></div>
outputs:
<div data-url="http://someurl.com/someimage.png?a=1234&b=5678"></div>
To add a little more context, my goal is not actually to use the URL in an anchor tag, since href
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 <object> <param>
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.
I am at a loss. Time to break out the MVC source code I guess...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…