如何为 ASP.net/C# 应用程序配置文件值中的值添加与号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/376135/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:09:35  来源:igfitidea点击:

How can I add an ampersand for a value in a ASP.net/C# app config file value

c#asp.netweb-config

提问by Rob Segal

I've got a C# program with values in a config file. What I want is to store ampersands for an url value like...

我有一个 C# 程序,在配置文件中有值。我想要的是为一个 url 值存储 & 符号,例如...

<appSettings>
  <add key="myurl" value="http://www.myurl.com?&cid=&sid="/>
</appSettings>

But I get errors building my site. The ampersand is not allowed. I've tried various forms of escaping the ampersands to no avail. Anyone know of the correct form to do this? All suggestions are welcome.

但是我在构建我的网站时出错。不允许使用与符号。我已经尝试了各种形式的转义符无济于事。任何人都知道正确的形式来做到这一点?欢迎提出所有建议。

采纳答案by Eric Rosenberger

Use "&amp;" instead of "&".

使用“ &amp;”代替“&”。

回答by ICR

I think you should be able to use the HTML escape character (&). They can be found at http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php

我认为您应该能够使用 HTML 转义字符 (&)。它们可以在http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php找到

回答by BenAlabaster

Have you tried this?

你试过这个吗?

<appSettings>  
  <add key="myurl" value="http://www.myurl.com?&amp;cid=&amp;sid="/>
<appSettings>

回答by user700390

Although the accepted answer here is technically correct, there seems to be some confusion amongst users based on the comments. When working with a ViewBag in a .cshtml file, you must use @Html.Rawotherwise your data, after being unescaped by the ConfigurationManager, will become re-escaped once again. Use Html.Raw()to prevent this from occurring.

尽管此处接受的答案在技术上是正确的,但根据评论,用户之间似乎有些混淆。在 .cshtml 文件中使用 ViewBag 时,您必须使用@Html.Raw否则您的数据,在被 ConfigurationManager取消转义后,将再次重新转义。使用Html.Raw(),以防止这种情况发生。