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="[email protected]" />
<add key="email2" value="[email protected]" />
<add key="email3" value="[email protected]" />
<add key="email4" value="[email protected]" />
</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.
As far as I know you cannot access the web.config inside the Views folder from controller, it is designed to be used in descending Views folders scope.
Anyway the error you are getting says that you are defining a duplicate section, so try to add your custom section after the existing sections in root web.config, for example: