0

I have an ASP.NET application that defines a custom configuration section in web.config.

Recently I had a customer who wanted to deploy two instances of the application (for testing in addition to an existing production application).

The configuration chosen by the customer was:

  • foo.com - production application
  • foo.com/Testing - test application

In this case, the ASP.NET configuration engine decided to apply the settings at foo.com/web.config to foo.com/Testing/web.config.

Thankfully this caused a configuration error because the section was redefined at the second level rather than giving the false impression that the two web applications were isolated.

What I would like to do is to specify that my configuration section is not inherited and must be re-defined for any web application that requires it but I haven't been able to find a way to do this.

My web.config ends up something like this

<configuration>
  <configSections>
    <section name="MyApp" type="MyApp.ConfigurationSection"/>
  </configSections>
  <MyApp setting="value" />
    <NestedSettingCollection>
      <Item key="SomeKey" value="SomeValue" />
      <Item key="SomeOtherKey" value="SomeOtherValue" />
    </NestedSettingCollection>
  </MyApp>
</configuration>
3
  • Can you explan this further "Thankfully this caused a configuration error because the section was redefined at the second level rather than giving the false impression that the two web applications were isolated." Commented May 2, 2010 at 22:37
  • The nested configuration setting was adding a value to the NestedSettingCollection with the same key as the value added in the parent configuration. This caused an exception "An entry with the same key already exists" rather than just taking the setting from the parent. Imagine what would happen if you had a connection string in the base application pointing to a production database and defined the connection string property in a subdirectory to point to a test database and found out just after you deleted a bunch of data that you had been pointing at production :( Commented May 2, 2010 at 22:42
  • I imagine that foo.com is the root the web site, with Testing setup as an application in that web site. ASP.NET will inherit the configuration of the web site, which is what you're experiencing. Commented May 3, 2010 at 0:07

2 Answers 2

1

Did you try using location element? Not sure if it works, but worth giving it a try. Put this in the web.config of the Testing project and try it out.

  <location path="." inheritInChildApplications="false">
    <MyApp setting="value" /> 
    ...
    </MyApp>    
  </location>

Two links that talk about using location element

http://www.aspdotnetfaq.com/Faq/how-to-disable-web-config-inheritance-for-child-applications-in-subfolders-in-asp-net.aspx

http://msdn.microsoft.com/en-us/library/b6x6shw7.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

This is an interesting approach. I was hoping to find a way to define my configuration setting to not inherit, but I might be able to change the default web.config to define the pertinent sections within a location section
+1 - I have used the location element to solve the exact the problem the author is experiencing. Just be careful though. Only wrap what you don't want inherited in the location tag. I wrapped most the web.config in the element and experienced an issue where the debugger would not attach to the process initially.
0

In the web.config under /testing, do this:

<configuration> 
  <configSections> 
    <remove name="MyApp"/> <===========
    <section name="MyApp" type="MyApp.ConfigurationSection"/> 
  </configSections> 
  <MyApp setting="value" /> 
    <NestedSettingCollection> 
      <Item key="SomeKey" value="SomeValue" /> 
      <Item key="SomeOtherKey" value="SomeOtherValue" /> 
    </NestedSettingCollection> 
  </MyApp> 
</configuration> 

1 Comment

This doesn't seem to change anything. I still get the same error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.