How to write an URI string in App.Config
Solution 1
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 &
, <
should be <
and >
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:
<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 );
}
Solution 2
&
should work just fine, Wikipedia has a List of predefined entities in XML.
Solution 3
Try using: &
in place of & in the url
radbyx
Backend developer at al dente. Former WebDeveloper at CapaSystems. I like to code in C#, MVC, JavaScript, Jquery, winforms, MSSQL, MySql ect. :) email: radbyx AT gmail DOT com
Updated on May 05, 2020Comments
-
radbyx about 4 years
I am making a
Windows Service
. TheService
has 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.I want to write an URI in my App.Config. What makes it invalid and how should i approach this?
<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
-
radbyx over 11 yearsThanks for the answers guys. You were first, so I'll accept yours, in a couple of minuts when the SO gods allow me too.