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

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

问题描述

我有我想在一个锚标记呈现为-处于的Razor视图的URL。我本来以为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>

但是,这并不正常工作。该符号获得EN codeD和HTML呈现为:

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>

但奇怪的是,如果我做的Html.Raw呼叫锚标记之外,如预期的HTML输出:

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>

将呈现为:

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

谁能解释这是为什么?

Can anyone explain why this is?

编辑:

尝试了一些其他的东西出来,发现预期了以下工作:

Tried a few other things out, and found that the following works as expected:

<div data-url="@Html.Raw(test)"></div>

输出:

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

要添加多一点的背景下,我的目标是不实际使用的URL中的锚标记,因为的href S可以是HTML EN codeD和仍然工作(我只是用锚标记作为一个例子)。而我想使用URL在&LT;对象&gt; &LT;&参数GT; 的Flash对象的价值标签。 Flash对象不能正确识别HTML EN codeD网址,但我不能正确地获取原始URL输出。

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.

我很茫然。时间打出来的MV​​C源$ C ​​$ C我猜...

I am at a loss. Time to break out the MVC source code I guess...

推荐答案

这是发生,因为一些在管道(我猜的剃须刀,但我需要看它)属性EN codeS好.. 。属性值。这不应该然而达到你想要的位置影响浏览器。

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.

您可以使用 @ Html.ActionLink(文字,动作,routeAttributes)过载测试。

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

输出

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


在问候你的编辑,你只需要使整个&LT;参数方式&gt; 您的原始值的一部分

@{
    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天全站免登陆