【问题标题】:Override machine.config by web.config通过 web.config 覆盖 machine.config
【发布时间】:2010-02-04 16:20:15
【问题描述】:

我学习使用 .Net 的内置配置文件提供程序,并拥有以下内容 问题:

我读到 machine.config-settings 可以被 web.config-settings 覆盖 .Net 应用程序。 machine.config 文件中的以下设置是相关的 对我来说:

<connectionStrings>
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;
Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

<profile><providers><add name="AspNetSqlProfileProvider"connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/></providers></profile>

这些设置用于设置本地配置文件。 但是,当我将设置复制到我的应用程序的 web.config 并更改 machine.config 设置,以便它们不再工作,我得到一个配置 错误。 例如,我将 machine.config 中的提供者名称更改为“Local”。 这应该没问题,因为设置被覆盖了。但是,运行时 我收到错误的应用程序:

“条目“AspNetSQLProvider 已添加”(我的翻译)

【问题讨论】:

    标签: asp.net web-config


    【解决方案1】:

    添加一个&lt;clear /&gt; 元素作为&lt;connectionStrings&gt; 的第一个子元素。这将导致配置系统忽略在machine.config 中添加的所有连接字符串并使用提供的新连接字符串。如果您不想清除整个配置项,也可以使用&lt;remove&gt; 元素删除单个配置项。

    <connectionStrings>
       <clear />
       <add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/> 
    </connectionStrings>
    

    同样的想法也适用于&lt;providers&gt; 部分。

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 2010-09-07
      • 2020-04-05
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 2011-02-27
      • 1970-01-01
      相关资源
      最近更新 更多