up vote 0 down vote favorite

This might be a god awful question but I'm not sure why it won't let me do this.

I have a URL I need to store in Web.config, which has a dynamic parameter pulled from the web page.

So I want to store:

        <add key="TestURL" 
value="https://test/subscribe?msisdn={0}&code=1&pass=2"/>

It doesn't let me do this. After the {0} it errors at the "&".

Can anyone let me know what I'm doing wrong here? Do I need to escape the character?

link|flag

have you tried escaping yet? – Jason Jul 16 '09 at 21:06

2 Answers

up vote 5 down vote accepted

Try this instead,

<add key="TestURL" value="https://test/subscribe?msisdn={0}&amp;code=1&amp;pass=2"/>

Notice the escaped ampersands.

link|flag
@JackM whatever values you want to place in the add tag value attribute just need to be HTML encoded. If you have a hard time figuring out how to encode a value properly you can write something that will have .Net do it for you using the HttpServerUtility.HtmlEncode method: msdn.microsoft.com/en-us/library/w3te6wfz.aspx – Spencer Ruport Jul 16 '09 at 21:16
up vote 1 down vote

Config files are XML, and as such, require XML entities to be escaped. The problem isn't your {0} for use in formatting, it's the & that must be escaped as

&amp;
link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.