Rob Segal
Rob Segal

Reputation: 7625

How can I add an ampersand for a value in a ASP.net/C# 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...

<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.

Upvotes: 243

Views: 121376

Answers (4)

user700390
user700390

Reputation: 2339

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.

Upvotes: 2

BenAlabaster
BenAlabaster

Reputation: 39864

Have you tried this?

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

Upvotes: 30

ICR
ICR

Reputation: 14162

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

Upvotes: 4

Eric Rosenberger
Eric Rosenberger

Reputation: 9117

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

Upvotes: 484

Related Questions