I'd like to store a set of key/value pairs in the application settings of my ASP.NET web app, but I'm not finding a straightforward way to do that. For example, these two questions tell me that StringDictionary etc. won't serialize to XML and suggest that I'll have to roll my own implementation. But it seems like this should be easier to do; after all, web.config is XML and < applicationSettings> is essentially a collection of key/value pairs, so it feels like I'm missing something obvious. Given my specific case below, do I really have to roll my own serialization, or is there an easier workaround?
The web app in question is a basic contact form that sends email to different recipients based on the value of a parameter; e.g. http://www.examplesite.com/Contact.aspx?recipient=support would send email to [email protected].
The goal is to be able add or remove recipients (or change their addresses) by editing the web.config file so that I don't have to recompile and can easily maintain different configurations in test and production environments. For example:
// I can use something like this for the sender address
SmtpMsg.From = New MailAddress(My.Settings.EmailSender)
// And then just edit this part of web.config to use
// different addresses in different environments.
<setting name="EmailSender" serializeAs="String">
<value>[email protected]</value>
</setting>
// I want something like this for the recipients
SmtpMsg.To.Add(My.Settings.Recipients("support"))
// and presumably some sort of equivalent xml in web.config
// maybe something like this???
<recipients>
<item name="support" serializeAs="String">
<value>[email protected]</value>
</item>
<!-- add or remove item elements here -->
</recipients>
edit: replaced VB comments w/ C# comments because of the code-coloring