使用inheritInChildApplications避免子Web应用程序中的web.config继承

Bla*_*man 148 asp.net configuration iis-7 web-config

我想补充一下

<location inheritInChildApplications="false">
Run Code Online (Sandbox Code Playgroud)

到我的父Web应用程序的web.config但它似乎没有工作.

我的父母web.config有:

<configuration>
    <configSections>
    </configSections>

    // 10 or so custom config sections like log4net, hibernate,

    <connectionStrings>
    </connectionStrings>

    <appSettings>
    </appSettings>

    <system.diagnostics>
    </system.diagnostics>

    <system.web>
         <webParts>
         </webParts>
         <membership>
         </membership>

         <compilation>
         </compilation>
    </system.web>

    <location ..>
    <system.web>
        </system.web>
    </location>

    <system.webServer>
    </system.webServer>
Run Code Online (Sandbox Code Playgroud)

我的子Web应用程序在IIS中设置为应用程序,并且继承了web.config导致问题的父级应用程序.

我应该在哪里放置

<location inheritInChildApplications="false">
Run Code Online (Sandbox Code Playgroud)

所以它忽略了所有各种web.config设置?

小智 196

正如前面提到的答案的评论者所说,你不能简单地添加这一行......

<location path="." inheritInChildApplications="false">
Run Code Online (Sandbox Code Playgroud)

......就在下面<configuration>.相反,您需要包装要禁用继承的各个web.config节.例如:

<!-- disable inheritance for the connectionStrings section -->
<location path="." inheritInChildApplications="false">
   <connectionStrings>
   </connectionStrings>
</location>

<!-- leave inheritance enabled for appSettings -->
<appSettings>
</appSettings>

<!-- disable inheritance for the system.web section -->
<location path="." inheritInChildApplications="false">
   <system.web>
        <webParts>
        </webParts>
        <membership>
        </membership>

        <compilation>
        </compilation>
      </system.web>
 </location>
Run Code Online (Sandbox Code Playgroud)

虽然<clear />可能适用于某些配置部分,但有些部分需要<remove name="...">指令,而其他部分似乎也不支持.在这些情况下,可能适合设置inheritInChildApplications="false".

  • 是否有可能以相反的方式做到这一点?我觉得奇怪的是,当孩子决定是否应该继承设置时,我必须更新父母. (9认同)
  • 我作为网站的子网站创建的应用程序仍然希望从父网站加载 DLL。显然,我不能将 `&lt;location&gt;` 用于运行时...... (2认同)

And*_*are 64

它需要直接进入<configuration>根节点,你需要设置这样的路径:

<?xml version="1.0"?>
<configuration>
    <location path="." inheritInChildApplications="false"> 
        <!-- Stuff that shouldn't be inherited goes in here -->
    </location>
</configuration>
Run Code Online (Sandbox Code Playgroud)

处理配置继承的更好方法是<clear/>在子配置中使用a ,而不想继承.因此,如果您不想继承父配置的连接字符串,您可以执行以下操作:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <clear/>
        <!-- Child config's connection strings -->
    </connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • 我收到此错误"无法读取配置部分'configSections',因为它在我的父web.config文件中缺少部分声明". (17认同)
  • -1:我还可以确认使用如上所示的location元素不起作用. (12认同)
  • 当你把它放在<configuration>下面时它不起作用.你可以把我们称为<system.web>节点,但你不能像这样把它放在root中. (6认同)

cry*_*yss 23

我把所有东西放进去:

<location path="." inheritInChildApplications="false">
....
</location>
Run Code Online (Sandbox Code Playgroud)

除了:<configSections/>,<connectionStrings/><runtime/>.

在某些情况下,我们不想继承某些部分<configSections />,但我们无法将<section/>标记放入<location/>,因此我们必须创建一个<secionGroup />并将不需要的部分放入该组中.稍后可以将节组插入位置标记.

所以我们必须改变这个:

<configSections>
  <section name="unwantedSection" />
</configSections>
Run Code Online (Sandbox Code Playgroud)

成:

<configSections>
  <sectionGroup name="myNotInheritedSections">
    <section name="unwantedSection" />
  </sectionGroup>
</configSections>

<location path="." inheritInChildApplications="false">
    <myNotInheritedSections>
        <unwantedSection />
    </myNotInheritedSections>
</location>
Run Code Online (Sandbox Code Playgroud)


Ken*_*rza 8

在最近向我们的某个开发环境发布代码之后,我们收到了与此相关的错误.我们有一个应用程序是另一个应用程序的子代.直到昨天,这种关系一直很好.

问题:
由于输入了重复的密钥,我们得到了黄色堆栈跟踪错误.这是因为子应用程序和父应用程序的web.config都具有此密钥.但这种存在多年来没有变化.为什么现在突然出现问题呢?

解决方案:
这不是问题的原因是因为键AND值始终相同.昨天我们更新了SQL连接字符串,以在连接字符串中包含Application Name.这使得字符串变得独一无二,突然之间开始失败.

在没有对此确切原因进行任何研究的情况下,我必须假设当子应用程序继承父web.config值时,它会忽略相同的键/值对.

我们能够像这样包装连接字符串来解决它

    <location path="." inheritInChildApplications="false">
        <connectionStrings>
            <!-- Updated connection strings go here -->
        </connectionStrings>
    </location>
Run Code Online (Sandbox Code Playgroud)

编辑:我忘了提到我在PARENTS web.config中添加了这个.我没有必要修改孩子的web.config.

感谢大家对此的帮助,救了我们的屁股.


Mat*_*tta 6

如果(据我所知)你试图在你的子应用程序的web配置中完全阻止继承,我建议你避免在web.config中使用该标记.而是创建一个新的apppool并编辑applicationHost.config文件(位于%WINDIR%\ System32\inetsrv\Config和%WINDIR%\ SysWOW64\inetsrv\config中).您只需找到您的apppool的条目并添加属性enableConfigurationOverride="false",如下例所示:

<add name="MyAppPool" autoStart="true" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated" enableConfigurationOverride="false">
    <processModel identityType="NetworkService" />
</add>
Run Code Online (Sandbox Code Playgroud)

这将避免MyAppPool提供的应用程序中的配置继承.

马特奥