C# 如何在 App.Config 中编写 URI 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12685846/
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
How to write an URI string in App.Config
提问by radbyx
I am making a Windows Service. The Servicehas to donwload something every night, and therefor I want to place the URI in the App.Config in case I later need to change it.
我正在制作一个Windows Service. 在Service每晚都要donwload东西,为此我要放置在App.config的URI的情况下,我以后需要改变它。
I want to write an URI in my App.Config. What makes it invalid and how should i approach this?
我想在我的 App.Config 中编写一个 URI。是什么使它无效,我应该如何处理?
<appSettings>
<add key="fooUriString"
value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"/>
</appSettings>
My errors:
我的错误:
- Entity 'login' not defined
- Expecting ';'
- Entity 'password' not defined
- Application Configuration file "App.config" is invalid. An error occurred
采纳答案by Dai
You haven't properly encoded the ampersands in your URI. Remember that app.configis an XML file, so you must conform to XML's requirements for escaping (e.g. &should be &, <should be <and >should be >).
您没有正确编码 URI 中的 & 符号。请记住,这app.config是一个 XML 文件,因此您必须符合 XML 的转义要求(例如&should be &、<should be<和>should be >)。
In your case, it should look like this:
在你的情况下,它应该是这样的:
<appSettings>
<add
key="fooUriString"
value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"
/>
</appSettings>
But in general, if you wanted to store a string that looked like "I <3 angle bra<kets & ampersands >>>"then do this:
但总的来说,如果你想存储一个看起来像"I <3 angle bra<kets & ampersands >>>"这样的字符串,请执行以下操作:
<appSettings>
<add
key="someString"
value="I <3 angle bra<kets & ampersands >>>"
/>
</appSettings>
void StringEncodingTest() {
String expected = "I <3 angle bra<kets & ampersands >>>";
String actual = ConfigurationManager.AppSettings["someString"];
Debug.Assert.AreEqual( expected, actual );
}
回答by Kapil Khandelwal
Try using: &in place of &in the url
尝试使用:&代替&在网址中
回答by Raab
&should work just fine, Wikipedia has a List of predefined entities in XML.
&应该可以正常工作,维基百科有一个XML 格式的预定义实体列表。

