【问题标题】:What is the purpose of configSections?configSections 的目的是什么?
【发布时间】:2010-01-15 16:53:13
【问题描述】:

我做了一些研究并遇到了这个问题:http://msdn.microsoft.com/en-us/library/ms228245.aspx

所以,如果我理解正确的话,最终它所做的是包含一些在项目中使用的 .dll,就像:

<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

我猜测不同之处在于,如果您以 configSections 方式执行此操作,则可以通过稍后在 webconfig(或其他配置)中将“名称”创建为 xml 元素来设置一些参数。这是正确的,还是我遗漏了什么?

另外,我注意到我可以从网站的 web.config 中删除一个 configSections,它会运行良好,特别是以下 configSections:

<configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
                <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
            </sectionGroup>
        </sectionGroup>
    </sectionGroup>
</configSections>

我在某处读到你可以这样做并让它仍然运行,因为在 machine.config 中默认也定义了 configSections。那么为什么要在网站的 web.config 中再次定义它呢?我假设用一些自定义设置覆盖 machine.config?有什么方法可以让我确定 machine.config 文件的默认内容是什么?

【问题讨论】:

    标签: .net web-config configsection


    【解决方案1】:

    你是对的。 ASP.NET 配置部分在machine.config 中定义。这是一个层次结构,每个配置文件都覆盖其父文件。您可以在以下目录下找到machine.config 和根web.config 文件。

    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG
    

    【讨论】:

    • 好的,谢谢,很高兴知道,但我也想知道这些东西到底是做什么的。例如,我知道如果我删除以下内容: 那么我将无法访问任何我网站中 System.Web.Extensions 中的类。如果我从 web.config 中删除这些配置部分会发生什么(并假设它们也不在 machine.config 中)
    • @Trajanus,抱歉耽搁了。正如您所说,machine.config 中未定义一些新的配置部分,例如 System.Web.Extensions。这些部分与 ASP.NET AJAX 服务相关。因此,如果您删除它们的定义,您将无法使用它们。如果您不需要它们,请删除此部分。
    【解决方案2】:

    您再次定义它以改变网站的行为。

    假设您正在运行多个不同的网站,并且您希望针对特定部分对它们进行不同的配置。这就是它们存在于 web.config 中的原因。

    【讨论】:

      【解决方案3】:

      所有 .NET Framework 应用程序都从名为 systemroot\Microsoft .NET\Framework\versionNumber\CONFIG\Machine.config 的文件继承基本配置设置和默认值。 Machine.config 文件用于服务器范围的配置设置。其中一些设置不能在层次结构中较低的配置文件中覆盖。

      .NET 客户端应用程序(控制台和 Windows 应用程序)使用名为 ApplicationName.config 的配置文件来覆盖继承的设置。 ASP.NET 应用程序使用名为 Web.config 的配置文件来覆盖继承的设置。

      ASP.NET 配置层次结构的根是一个称为根 Web.config 文件的文件,它与 Machine.config 文件位于同一目录中。根 Web.config 文件继承 Machine.config 文件中的所有设置。根 Web.config 文件包含适用于所有运行特定版本 .NET Framework 的 ASP.NET 应用程序的设置。因为每个 ASP.NET 应用程序都从根 Web.config 文件继承默认配置设置,所以您只需为覆盖默认设置的设置创建 Web.config 文件。

      ASP.NET Configuration File Hierarchy and Inheritance

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-05
        • 2012-08-01
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        • 2019-07-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多