开发者

Multiple appSettings section in web.config

Is this possible in anyway? For example through having named appSettings sections, or appSettings nested in other named sections.

I want to achieve something like the following:

&l开发者_JS百科t;section name="development">
    <appSettings>
    </appSettings>
</section>

<section name="test">
    <appSettings>
    </appSettings>
</section>


string connectionString 
   = ConfigurationManager.GetSection("test").AppSettings["connectionString"];

What is the pattern for this?


Assuming that what you're trying to do is simply apply a different connection string (or manipulate other web.config settings) when deploying to different environments, what you're looking for is config transforms. This is fastest, easiest and correct way to handle this situation.


The appSetting element supports a "file" attribute, which lets you specify a filename were key/values can be placed.

This lets you not share with the team the values you've specified in user.config.

I think, if you define a single key in both the parent and child config files, the child value will be ignored and the parent value will be respected.

Further Reading


What you are trying to achieve will not work this way. The "best" (IMHO) technique for this is to create 3 files app(or web).config dev.config and test.config (and perhaps release.config). Then in the solution properties you can execute a pre-build command to copy the appropriate .config file so that it becomes "active" based on the build type, into the app.config (or web.config).

you can learn more about this http://msdn.microsoft.com/en-us/library/aa983464%28v=vs.80%29.aspx and http://msdn.microsoft.com/en-us/library/ke5z92ks.aspx

Scott Hanselman has a great article on this approach: http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

So you need to do this at runtime:

If you want to get different settings at run time then the approach that will probably work best is to establish a naming convention like:

<appSettings>
   <add name="fearofawhackplanet.connectionString" value="your connection string" />
   <add name="cosCallis.connectionString" value="my connection string" />
</appSettings>

I trust from there you can see how to manage this at runtime to get the key/values you need.


I found custom sections to be most flexible for that. See "configSections Element (General Settings Schema)" in MSDN.

<sampleSection serverInstanceLabel1="alex" setting1="Value1"
               setting2="value two"/> 

So, in your case you can have sections like:sampleSection,sampleSection1,...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜