We help IT Professionals succeed at work.

Ampersand in Web.Config appSettings

I am using the AppSettings section of my Web.Config file to link a URL with a key value.  It works perfectly for every url I have except when an ampersand (&) is contained in the key value or url.  Does anyone know a way around this?  I have done a little research and have found suggestions to use & in place of the ampersand but that does not seem to work.  Any idea? Thanks.
<add key="RegScreen23" value="http://testurl/winmobile/MrPerformanceFuel_bb.asp"/>
    <add key="RegKey23" value="Meramec Performance Fuel"/>
    <add key="RegScreen24" value="http://testurl/winmobile/MrChemOverview1&amp;2_bb.asp"/>
    <add key="RegKey24" value="Meramec U1&amp;2 Chemistry"/>
    <add key="RegScreen25" value="http://testurl/winmobile/MrChemOverview3&amp;4_bb.asp"/>
    <add key="RegKey25" value="Meramec U3&amp;4 Chemistry"/>

Open in new window

Comment
Watch Question

Top Expert 2015

Commented:
Have you tried CDATA?
value="<![CDATA[" http://testurl/winmobile/MrChemOverview3&4_bb.asp"]]>"
Commented:
Hi rkneal,

Technicaly the &amp should work if the compiler wouldnt fail to put in URL-format instead of HTML-format!
So instead of using &amp try using %26, %26 is the URL-encoding of the char &

Example below in code!

Greetz!

ps: let me know if it works or not, i'm not on a testing pc for the moment!
<add key="RegScreen23" value="http://testurl/winmobile/MrPerformanceFuel_bb.asp"/>
    <add key="RegKey23" value="Meramec Performance Fuel"/>
    <add key="RegScreen24" value="http://testurl/winmobile/MrChemOverview1%262_bb.asp"/>
    <add key="RegKey24" value="Meramec U1&amp;2 Chemistry"/>
    <add key="RegScreen25" value="http://testurl/winmobile/MrChemOverview3%264_bb.asp"/>
    <add key="RegKey25" value="Meramec U3%264 Chemistry"/>

Open in new window

Commented:
The web.config file is XML so you have to encode the & char: &amp;
Struggling to see the problem - &amp; should encode correctly. What's the exact compiler error you are seeing?

Author

Commented:
I don't see any errors, what happens is it compiles fine but when I run it, it cuts off the url at the &.  So instead of getting a url of ...

http://testurl/winmobile/MrChemOverview3&4_bb.asp

I get...

http://testurl/winmobile/MrChemOverview3

Then it errors that can't find the page due to the URL being incomplete.
Top Expert 2015

Commented:
A quote from w3schools.com (http://www.w3schools.com/XML/xml_cdata.asp)

...CDATA - (Unparsed) Character Data
The term CDATA is used about text data that should not be parsed by the XML parser.

Characters like "<" and "&" are illegal in XML elements.

"<" will generate an error because the parser interprets it as the start of a new element.

"&" will generate an error because the parser interprets it as the start of an character entity.

Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Everything inside a CDATA section is ignored by the parser.

A CDATA section starts with "<![CDATA[" and ends with "]]>": ...

...So, &amp; cannot work either.

Commented:
If you really can't get the encoding to work, you can work around it, use : instead and replace:

-- web.config --
<add key="RegScreen24" value="http://testurl/winmobile/MrChemOverview1:2_bb.asp"/>

-- Code --
Dim url As String = ConfigurationManager.AppSettings("RegScreen24").ToString.Replace(":", "&")
>>Then it errors that can't find the page due to the URL being incomplete.<<

Forgive me if I have the wrong end of the stick here - as I understand it your app displays a link? Clicking on this link fails because it cuts off? Or, does the app redirect the user to this page and the redirect fails?

Commented:
i am pretty sure the %26 is the solution will work,
http://discuss.techinterview.org/default.asp?dotnet.12.414245.11
feedback?

Greetz!

Author

Commented:
I am trying the different solutions now.  Some brief background info on what I am doing...
I am monitoring a mailbox, when a user sends a message to the mailbox I grab the TO, FROM, and SUBJECT from the message.  I check the Subject for a match in the RegKeyX section of the web.config file.  If I detect a match, I go out to the the URL in the Web.Config (RegScreenX), and basically take a screen shot of the page.  I then create a message and embed the SS into the Body and reply to the sender.    I am designing this for users who don't have access to my intranet to see current status of certain pages when they need to.
Monitoring a mailbox and using a web.config file? Seems like you are using the wrong tool for the job if you ask me - firstly - monitoring should be done in a Windows service or an executable run using Windows Scheduled Tasks - how are you scheduling a website to run every so often? Also, really would consider storing these values outside the web.config file - maybe a database? If you need to change these you would force the website to recompile when you modify the web.config which would kick all your users off the site and kill any session variables :(

Author

Commented:
I am using POP3S, and then calling my Web Service when a new message arrives.  The information about what I am doing was just a brief overview of the big picture.  I have it working exactly how I need it, am just having issues with the & in the appSettings of the Web.Config for the web service.  Sorry, not trying to get everyone all caught up in the entire process, just what I am having trouble with.
Top Expert 2015

Commented:
Web.config files do not get compiled with the website. It's a good place to store values that may change on rare occasions like SQL connection strings, network settings, etc..

Author

Commented:
I had two locations with issues on this problem.  In the Web.Config file the &amp; was the correct to put in place of the ampersand(&).  

I also had links in an HTML document that automatically created an email message with the TO and Subject line already populated.  In this case using the %26 in place of the & worked correctly to not cut off the Subject.

Thanks!