How can I add an ampersand for a value in a ASP.net/C# app config file value

C#asp.netWeb Config

C# Problem Overview


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

<appSettings>
  <add key="myurl" value="http://www.myurl.com?&cid=&sid="/>
</appSettings>

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.

C# Solutions


Solution 1 - C#

Use "&amp;" instead of "&".

Solution 2 - C#

Have you tried this?

<appSettings>  
  <add key="myurl" value="http://www.myurl.com?&amp;cid=&amp;sid="/>
<appSettings>

Solution 3 - C#

I think you should be able to use the HTML escape character (&). They can be found at http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php

Solution 4 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRob SegalView Question on Stackoverflow
Solution 1 - C#Eric RosenbergerView Answer on Stackoverflow
Solution 2 - C#BenAlabasterView Answer on Stackoverflow
Solution 3 - C#ICRView Answer on Stackoverflow
Solution 4 - C#user700390View Answer on Stackoverflow