为什么 Html.Raw 在 ASP.NET MVC 4 中的锚标记中转义与号? [英] Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?

查看:23
本文介绍了为什么 Html.Raw 在 ASP.NET MVC 4 中的锚标记中转义与号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 URL,我想在 Razor 视图中按原样呈现在锚标记中.我原以为 Html.Raw 会是要走的路:

@{字符串测试 = "http://someurl.com/someimage.png?a=1234&b=5678";}<div><a href="@Html.Raw(test)">Test</a>

但这行不通.&符号被编码,HTML呈现为:

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

奇怪的是,如果我在锚标记之外执行 Html.Raw 调用,则 HTML 输出符合预期:

@Html.Raw(测试)<div>

呈现为:

http://someurl.com/someimage.png?a=1234&b=5678

谁能解释一下这是为什么?

尝试了其他一些东西,发现以下效果如预期:

输出:

<div data-url="http://someurl.com/someimage.png?a=1234&b=5678"></div>

要添加更多上下文,我的目标实际上并不是在锚标记中使用 URL,因为 href 可以进行 HTML 编码并且仍然有效(我仅以锚标记案例为例).相反,我希望在 中使用 URL. Flash 对象的值标记.Flash 对象无法正确识别 HTML 编码的 URL,但我无法正确输出原始 URL.

我不知所措.我猜是时候打破 MVC 源代码了...

解决方案

发生这种情况是因为管道中的某些东西(我猜是 razor,但我需要查找它)属性对所有属性值进行编码.但是,这不会影响浏览器到达您想要的位置.

您可以使用 @Html.ActionLink(text, action, routeAttributes) 重载来测试这一点.

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

输出

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


关于您的编辑,您只需要将整个 <param> 作为原始值的一部分.

@{var test = "<param src="http://someurl.com/someimage.png?a=1234&b=5678">";}<div><对象>@Html.Raw(测试)</对象>

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&amp;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 hrefs 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...

解决方案

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>

这篇关于为什么 Html.Raw 在 ASP.NET MVC 4 中的锚标记中转义与号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆