web.config 中的多个 appSettings 部分
无论如何这可能吗?例如,通过命名 appSettings 部分,或将 appSettings 嵌套在其他命名部分中。
我想实现如下目标:
<section name="development">
<appSettings>
</appSettings>
</section>
<section name="test">
<appSettings>
</appSettings>
</section>
string connectionString
= ConfigurationManager.GetSection("test").AppSettings["connectionString"];
这个的模式是什么?
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:
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您尝试做的只是在部署到不同环境时应用不同的连接字符串(或操作其他 web.config 设置),那么您正在寻找的是 配置转换。这是处理这种情况的最快、最简单且正确的方法。
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.
appSetting 元素支持“file”属性,它允许您指定可以放置键/值的文件名。
这样您就可以不与团队共享您在 user.config 中指定的值。
我认为,如果您在父配置文件和子配置文件中都定义了单个键,则子值将被忽略,父值将被尊重。
进一步阅读
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
你想要实现的目标不会以这种方式实现。 “最好的”(恕我直言)技术是创建 3 个文件 app(或 web).config dev.config 和 test.config(也许还有release.config)。然后,在解决方案属性中,您可以执行预构建命令以将相应的 .config 文件复制到 app.config(或 web.config)中,以便根据构建类型使其变为“活动”状态。
您可以了解有关此的更多信息 http://msdn .microsoft.com/en-us/library/aa983464%28v=vs.80%29.aspx
和http://msdn.microsoft.com/en-us/library/ke5z92ks。 aspx
Scott Hanselman 有一篇关于这种方法的精彩文章:http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
所以你需要在运行时执行此操作:
如果您想在运行时获得不同的设置,那么最有效的方法可能是建立命名约定就像:
我相信从那里您可以看到如何在运行时管理它以获得您需要的键/值。
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:
I trust from there you can see how to manage this at runtime to get the key/values you need.
我发现自定义部分对此最为灵活。请参阅 MSDN 中的“configSections 元素(常规设置架构)”。
因此,在您的情况下,您可以有以下部分:sampleSection,sampleSection1,...
I found custom sections to be most flexible for that. See "configSections Element (General Settings Schema)" in MSDN.
So, in your case you can have sections like:sampleSection,sampleSection1,...