How to write an URI string in App.Config

前端 未结 3 589
终归单人心
终归单人心 2021-02-06 22:35

I am making a Windows Service. The Service has to donwload something every night, and therefor I want to place the URI in the App.Config in case I late

相关标签:
3条回答
  • 2021-02-06 23:11

    & should work just fine, Wikipedia has a List of predefined entities in XML.

    0 讨论(0)
  • 2021-02-06 23:18

    Try using: & in place of & in the url

    0 讨论(0)
  • 2021-02-06 23:20

    You haven't properly encoded the ampersands in your URI. Remember that app.config is an XML file, so you must conform to XML's requirements for escaping (e.g. & should be &amp;, < should be &lt; and > should be &gt;).

    In your case, it should look like this:

    <appSettings>
        <add
            key="fooUriString" 
            value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&amp;login=null&amp;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:

    <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 );
    }
    
    0 讨论(0)
提交回复
热议问题