【问题标题】:Add more than one relative path in webconfig appSetting in asp.net在 asp.net 的 web config appSetting 中添加多个相对路径
【发布时间】:2013-11-23 01:55:07
【问题描述】:

如何在 asp.net web.config 文件的 appSetting 路径属性中指定多个相对文件路径。我的 appsetting 密钥位于两个不同的文件中。

<appSettings file="Web.User.config">
 </appSettings >  

【问题讨论】:

标签: asp.net


【解决方案1】:

您不能为 appSettings 引用多个文件路径。 appSettings 是一个部分,您不能拥有多个部分。但是,您可以添加一个与 appSettings 部分类似的新部分。示例:

<configuration>
      <configSections>
        <section name="CustomConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      </configSections>
    ....
<CusTomConfig>
    <add key="Key1" value="Value1"/>
</CustomConfig>

然后,您可以通过以下方式访问该部分:

NameValueCollection settings = (NameValueCollection)ConfigurationManager.GetSection("CustomConfig"); 

第二种选择是使用Custom Configuration Section。这将需要一些样板代码(如 MSDN 文档中所示),但您将能够使用属性访问设置值,而不是使用像 settings["Key1"] 这样的字符串来引用它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多