ASP.NET自动转换和放大器;到&放大器;放大器; [英] ASP.NET automatically converts & to &

查看:170
本文介绍了ASP.NET自动转换和放大器;到&放大器;放大器;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小问题,但它的驾驶我坚果不过。

Minor issue, but it's driving me nuts nonetheless.

我要建一个URL的<脚本> 标记包括ASP.NET页面上呈现,是这样的:

I'm building a url for a <script> tag include to be rendered on an ASP.NET page, something like this:

<script src='<%= string.Format("http://example.com/page.aspx?a={0}&b={1}&c={2:0.00}", A, B, C)%>' type='text/javascript'></script>

问题是,当这个被渲染,在&安培; 字符被替换成&放大器;放大器;

<script src='http://example.com/page.aspx?a=xxx&amp;b=zzz&amp;c=123.45' type='text/javascript'></script>

我期待这一点,显然是:

I was expecting this, obviously:

<script src='http://example.com/page.aspx?a=xxx&b=zzz&c=123.45' type='text/javascript'></script>

不过,如果我直接呈现的网址,外&LT;脚本&GT; 标签,它看起来OK了!只是做

However, if I render the url directly, outside the <script> tag, it looks ok! Just doing

<%= string.Format("http://example.com/page.aspx?a={0}&b={1}&c={2:0.00}", A, B, C) %>

此呈现:

http://example.com/page.aspx?a=xxx&b=zzz&c=123.45

怎么办?我如何制止这种疯狂?我的OCD不能把它!

What gives? And how do I stop this madness? My OCD can't take it!

推荐答案

由于@Falkon和@AVD说,ASP.NET会自动在做正确的事情&LT;脚本&GT; 标记。请参阅W3C建议 - C.12。在属性值中使用符号(和其他地方)

As @Falkon and @AVD have said, ASP.NET is automatically doing the "right" thing in the <script> tag. See the w3c recommendation - C.12. Using Ampersands in Attribute Values (and Elsewhere)

为了确保文件是与要处理一个文档中使用的历史HTML用户代理和基于XML的用户代理,&号兼容,因为文字字符必须是前pressed自己作为一个实体引用(如: &放大器;放大器; )。

In order to ensure that documents are compatible with historical HTML user agents and XML-based user agents, ampersands used in a document that are to be treated as literal characters must be expressed themselves as an entity reference (e.g. "&amp;").

我不完全知道为什么ASP.NET不会做同样的事情在页面的其余部分(可以是任何数量的很好的理由),但至少它纠正脚本标记的符号。结论:虽然你可能会骂的ASP.NET,争先恐后您的网址,你可能要感谢它,而不是帮助你的网页是符合标准的。

I'm not entirely sure why ASP.NET doesn't do the same thing in the rest of the page (could be any number of good reasons), but at least it's correcting the ampersand in the script tag. Conclusion: While you may be cursing ASP.NET for "scrambling" your url, you may want to thank it instead for helping your webpage be standards compliant.

这篇关于ASP.NET自动转换和放大器;到&放大器;放大器;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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