How can I add an ampersand for a value in a app config file value

I’ve got a C# program with values in a config file. What I want is to store ampersands for an url value like…

  <add key="myurl" value=""/>

But I get errors building my site. The ampersand is not allowed. I’ve tried various forms of escaping the ampersands to no avail. Anyone know of the correct form to do this? All suggestions are welcome.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use “&amp;” instead of “&”.

Method 2

Have you tried this?

  <add key="myurl" value=";cid=&amp;sid="/>

Method 3

I think you should be able to use the HTML escape character (&). They can be found at

Method 4

Although the accepted answer here is technically correct, there seems to be some confusion amongst users based on the comments. When working with a ViewBag in a .cshtml file, you must use @Html.Raw otherwise your data, after being unescaped by the ConfigurationManager, will become re-escaped once again. Use Html.Raw() to prevent this from occurring.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x