0

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.

Community
  • 1
  • 1
Daniel Grima
  • 2,765
  • 7
  • 34
  • 58

1 Answers1

0

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:

<configuration>
  <configSections>
    <!-- YOUR SECTION -->
    <section name="AdminEmails" type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <!-- EXISTING SECTIONS -->
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"></sectionGroup>
    </sectionGroup>
  </sectionGroup>
</configSections>
Daniele
  • 1,938
  • 16
  • 24
  • Thanks for your reply. But the weird thing is that in my root web.config file there isn't a tag it only exists in the views web.config file. However I was getting the 'duplicate section' error. It didn't occur after I put the custom sections in the views web.config file. – Daniel Grima Sep 11 '13 at 11:04