I'm working on an MVC application and in a particular section of the website I need to send notification emails (I'm guessing maybe a maximum of 10). So I thought that I'd save the emails as a list in the web.config file and then loop through the list to send emails.
I first tired creating a custom section and adding the data I needed (in the main web.config file) like so (Reference):
My Code:
web.config file:
<configSections>
<section
name="AdminEmails"
type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<AdminEmails>
<add key="email1" value="test@test.com" />
<add key="email2" value="abc@abc.com" />
<add key="email3" value="email@email.com" />
<add key="email4" value="username@domain.com" />
</AdminEmails>
Code in controller:
NameValueCollection section =
(NameValueCollection)ConfigurationManager.GetSection("AdminEmails");
//... loop through emails in 'AdminEmails' section...
But then I got an error stating that there cannot be duplicate <configSection>
as there already was a <configSection>
in the other web.config file. So instead I added the data in the web.config file saved in the views folder. The website ran however the section
variable was null
. I'm thinking this is because ConfigurationManager.GetSection()
tried to get a section from the 'main' web.config file. I don't know if it's possible a way to access the web.config file saved in the views folder via code.