【问题标题】:How to write an URI string in App.Config如何在 App.Config 中编写 URI 字符串
【发布时间】:2012-09-23 01:03:42
【问题描述】:

我正在制作Windows ServiceService 必须每晚下载一些东西,因此我想将 URI 放在 App.Config 中,以防我以后需要更改它。

我想在我的 App.Config 中编写一个 URI。是什么使它无效,我应该如何处理?

<appSettings>
    <add key="fooUriString" 
         value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"/>
</appSettings>

我的错误:

- Entity 'login' not defined
- Expecting ';'
- Entity 'password' not defined
- Application Configuration file "App.config" is invalid. An error occurred

【问题讨论】:

标签: c# configuration windows-services uri app-config


【解决方案1】:

您没有正确编码 URI 中的 & 符号。记住app.config是一个XML文件,所以你必须符合XML的转义要求(例如&amp;amp;应该是&amp;amp;&amp;lt;应该是&amp;lt;&amp;gt;应该是&amp;gt;)。

在你的情况下,它应该是这样的:

<appSettings>
    <add
        key="fooUriString" 
        value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&amp;login=null&amp;password=null"
    />
</appSettings>

但一般来说,如果您想存储一个看起来像 "I &lt;3 angle bra&lt;kets &amp; ampersands &gt;&gt;&gt;" 的字符串,请执行以下操作:

<appSettings>
    <add
        key="someString"
        value="I &lt;3 angle bra&lt;kets &amp; ampersands &gt;&gt;&gt;"
    />
</appSettings>

void StringEncodingTest() {
    String expected = "I <3 angle bra<kets & ampersands >>>";
    String actual   = ConfigurationManager.AppSettings["someString"];
    Debug.Assert.AreEqual( expected, actual );
}

【讨论】:

  • 感谢各位的回答。你是第一个,所以我会在几分钟内接受你的,当 SO 诸神也允许我时。
【解决方案2】:

&amp;amp; 应该可以正常工作,维基百科有一个List of predefined entities in XML

【讨论】:

    【解决方案3】:

    尝试使用:&amp;amp; 代替网址中的 &

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 2014-06-15
      • 2020-02-13
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 2014-05-20
      • 1970-01-01
      相关资源
      最近更新 更多