visual-studio 如何防止继承“configSections”的 web.config 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4030891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:39:27  来源:igfitidea点击:

How to prevent inheritance for web.config file for "configSections"?

c#asp.netvisual-studiovisual-studio-2010

提问by DSharper

I have following in my parent web applications config file

我在父 Web 应用程序配置文件中有以下内容

<configuration>
  <configSections>
    <sectionGroup name="testmodule">
      <section name="testmodule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
    </sectionGroup>
  </configSections>
</configuration>

i want to prevent child subfolders from inheriting this config section where should i put <location path="." inheritInChildApplications="false">, since config sections should be first child element of configuration file

我想防止子文件夹继承这个配置部分我应该放在哪里 <location path="." inheritInChildApplications="false">,因为配置部分应该是配置文件的第一个子元素

采纳答案by DSharper

Seems to be no solution for this currently, should avoid using conflicting section groups in web.config file.

目前似乎没有解决方案,应避免在 web.config 文件中使用冲突的节组。

回答by Nariman

This has been answered a couple of times on SO, but incorrectly in my opinion.

这已经在 SO 上回答了几次,但在我看来是错误的。

The docs, are pretty clear (1)(2):

文档非常清楚 (1)(2):

The following example shows how to use this attribute in a configuration file to specify that the settings defined in the location element for the root of a Web site should not be inherited by child applications:

The InheritInChildApplications property applies only to location-specific configuration settings.

以下示例显示如何在配置文件中使用此属性来指定在位置元素中为网站根目录定义的设置不应被子应用程序继承:

InheritInChildApplications 属性仅适用于特定于位置的配置设置。

To answer your question, this should suffice:

要回答您的问题,这应该足够了:

<configuration>
...
<sectionGroup name="testmodule">
    <section name="testmodule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
</sectionGroup>
...
<location path="." inheritInChildApplications="false">
    <testModule>
    ....
    </testModule>
</location>

(1) - http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.inheritinchildapplications.aspx

(1) - http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.inheritinchildapplications.aspx

(2) - http://msdn.microsoft.com/en-us/library/ms178692.aspx

(2) - http://msdn.microsoft.com/en-us/library/ms178692.aspx

回答by mickdelaney

If you use a different name in the conflicting child section it will not clash with the parent and you can reference both sections in their respective web.config files using the different names..

如果您在冲突的子部分中使用不同的名称,它不会与父部分发生冲突,您可以使用不同的名称在各自的 web.config 文件中引用这两个部分。

its not perfect but its a work around i've used with success....

它并不完美,但它是我成功使用的一种解决方法......

回答by Josh

We're getting errors about duplicate configuration directives on the one of our apps. After investigation it looks like it's because of this issue.

我们在我们的应用程序之一上收到有关重复配置指令的错误。经过调查,看起来是因为这个问题

In brief, our root website is ASP.NET 3.5 (which is 2.0 with specific libraries added), and we have a subapplication that is ASP.NET 4.0.

简而言之,我们的根网站是 ASP.NET 3.5(添加了特定库的 2.0),我们有一个子应用程序是 ASP.NET 4.0。

web.config inheritance causes the ASP.NET 4.0 sub-application to inherit the web.config file of the parent ASP.NET 3.5 application.

web.config 继承会导致 ASP.NET 4.0 子应用程序继承父 ASP.NET 3.5 应用程序的 web.config 文件。

However, the ASP.NET 4.0 application's global (or "root") web.config, which resides at C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config and C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config (depending on your bitness), already contains these config sections.

但是,ASP.NET 4.0 应用程序的全局(或“根”)web.config 位于 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config 和 C:\Windows\Microsoft。 NET\Framework64\v4.0.30319\Config\web.config(取决于您的位数),已经包含这些配置部分。

The ASP.NET 4.0 app then tries to merge together the root ASP.NET 4.0 web.config, and the parent web.config (the one for an ASP.NET 3.5 app), and runs into duplicates in the node.

ASP.NET 4.0 应用程序然后尝试将根 ASP.NET 4.0 web.config 和父 web.config(用于 ASP.NET 3.5 应用程序的)合并在一起,并在节点中遇到重复项。

The only solution I've been able to find is to remove the config sections from the parent web.config, and then either

我能找到的唯一解决方案是从父 web.config 中删除配置部分,然后

  1. Determine that you didn't need them in your root application, or
  2. Upgrade the parent app to ASP.NET 4.0 (so it gains access to the root web.config's configSections)
  1. 确定您的根应用程序中不需要它们,或者
  2. 将父应用升级到 ASP.NET 4.0(因此它可以访问根 web.config 的 configSections)

回答by nikademus

i have the same situation than you as my root Application is using an AppPool of .net 2.0 and child AppPool is .Net 4.0. I solved using the suggestion in "Entry has already been added" - Two Separate App Pools, by setting both AppPools to enableConfigurationOverride="false" it works like a charm.

我的情况与您相同,因为我的根应用程序使用的是 .net 2.0 的 AppPool,而子 AppPool 是 .Net 4.0。我解决了使用“条目已经添加”中的建议- 两个单独的应用程序池,通过将两个 AppPools 设置为 enableConfigurationOverride="false" 它就像一个魅力。