vb.net ASP.NET 自动将 & 转换为 &
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13737902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET automatically converts & to &
提问by Jakob Gade
Minor issue, but it's driving me nuts nonetheless.
小问题,但它仍然让我发疯。
I'm building a url for a <script>tag include to be rendered on an ASP.NET page, something like this:
我正在<script>为要在 ASP.NET 页面上呈现的标记构建一个 URL ,如下所示:
<script src='<%= string.Format("http://example.com/page.aspx?a={0}&b={1}&c={2:0.00}", A, B, C)%>' type='text/javascript'></script>
Problem is when this is rendered, the &characters are replaced with &:
问题是当它被渲染时,&字符被替换为&:
<script src='http://example.com/page.aspx?a=xxx&b=zzz&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>
However, if I render the url directly, outside the <script>tag, it looks ok! Just doing
但是,如果我直接在<script>标签外渲染 url ,它看起来没问题!只是在做
<%= string.Format("http://example.com/page.aspx?a={0}&b={1}&c={2:0.00}", A, B, C) %>
Renders this:
渲染这个:
http://example.com/page.aspx?a=xxx&b=zzz&c=123.45
What gives? And how do I stop this madness? My OCD can't take it!
是什么赋予了?我该如何阻止这种疯狂?我的强迫症受不了!
采纳答案by Michael Richardson
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)
正如@Falkon 和@AVD 所说,ASP.NET 会自动在<script>标签中做“正确”的事情。请参阅 w3c 建议 - C.12。在属性值(和其他地方)中使用与号
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. "
&").
为了确保文档与历史 HTML 用户代理和基于 XML 的用户代理兼容,文档中使用的被视为文字字符的 & 符号本身必须表示为实体引用(例如“
&”)。
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 在页面的其余部分不做同样的事情(可能有很多好的理由),但至少它正在更正脚本标记中的&符号。结论:虽然您可能会因为 ASP.NET “扰乱”您的 url 而诅咒 ASP.NET,但您可能要感谢它帮助您的网页符合标准。
回答by Kaizen Programmer
Maybe MvcHtmlString.Create()or Html.Raw()?
也许MvcHtmlString.Create()或Html.Raw()?
<script src='<%= MvcHtmlString.Create("http://example.com/page.aspx?a={0}&b={1}&c={2:0.00}", A, B, C)%>' type='text/javascript'></script>
or
或者
<script src='<%= Html.Raw("http://example.com/page.aspx?a={0}&b={1}&c={2:0.00}", A, B, C)%>' type='text/javascript'></script>
回答by Joel Coehoorn
Instead of <%=, which can sometimes automatically HTMLEncode things it writes to the response stream, try using <$:. This is a new(ish) code expression nugget syntax added with ASP.Net 4.0. This new syntax will still HTMLEncode most things by default, but there is a special IHtmlStringinterface you can use to explicitly tell this new nugget that you do notwant to HTMLEncode this data, and thus avoid double encoding. You should pretty much always use the newer <%:and pretty much never use the older <%=... though of course there will be exceptions to this.
相反的<%=,其中,有时会自动的HTMLEncode的事情写入响应流,请尝试使用<$:。这是 ASP.Net 4.0 添加的新(ish)代码表达式块语法。这种新的语法仍然会的HTMLEncode大多数事情默认,但有一个特殊的IHtmlString,你可以用它来明确地告诉这个新的金块,你的界面不希望的HTMLEncode这些数据,从而避免双重编码。您几乎应该总是使用较新的,<%:而几乎从不使用旧的<%=……当然,这也有例外。
More details are available here:
可在此处获得更多详细信息:
回答by Situs Panesse
I can work it out. I just make a method:
我可以解决。我只是做了一个方法:
public void BuildUrl(String baseUrl = "", String data = "")
{
Response.Write(baseUrl + data);
}
and use it in my html page like this:
并在我的 html 页面中使用它,如下所示:
<button type="button" class="btn_new" ref="<% this.BuildUrl(this.BaseUrl + "Master/Tanker_Edit.aspx?", "type=new&unique_id=" + Session.SessionID); %>">New</button>
The result:
结果:
<button type="button" class="btn_new" ref="http://kideco.local/Master/Tanker_Edit.aspx? type=new&unique_id=emxw1pkpnwcxpn1cl2cf04zv">
回答by ElijahGartin
"&" is a reserved character in HTML and XML, and consequently ASP.NET. The "&" gets converted by ASP.NET to & because that is the code to display that character on the web.
“&”是 HTML 和 XML 中的保留字符,因此也是 ASP.NET 中的保留字符。“&”被 ASP.NET 转换为& 因为这是在网络上显示该字符的代码。
You might find your answer in the answers on this question : ASP.Net URLEncode Ampersand for use in Query String
您可能会在以下问题的答案中找到答案:ASP.Net URLEncode Ampersand for use in Query String
Hope that helps, good luck!
希望能帮到你,祝你好运!
回答by obfk
Around the comment use Server.HtmlEncode( yourString )
围绕评论使用 Server.HtmlEncode( yourString )
It will automatically escape the double or single quotes for you, as well as ampersands (&) and less-than and greater-than signs, etc.
它将自动为您转义双引号或单引号,以及与号 (&) 和小于和大于号等。

