configSections的目的是什么?

4
我进行了一些研究,发现了这个网站:http://msdn.microsoft.com/en-us/library/ms228245.aspx 如果我理解得正确的话,最终它做的是在项目中包含了一些 .dlls,就像这样:
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
我猜区别在于如果你用 configSections 的方式,你可以通过在 webconfig(或其他配置文件)中创建“name”作为 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>

我在某处读到,即使在机器配置文件中默认定义了configSections,你仍然可以这样做并使其运行。那么为什么要在网站的web.config中重新定义它呢?我猜想是为了用一些自定义设置覆盖机器配置文件吗?有没有办法让我确定machine.config文件的默认内容是什么?

3个回答

1

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

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

好的,谢谢你告诉我这些信息,但我还想知道这些东西具体是做什么的。例如,我知道如果我删除像这样的内容:<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>那么我将无法在我的网站中访问 System.Web.Extensions 中的任何类。如果我从我的 web.config 中删除这些配置部分(并且假设它们也不在 machine.config 中),会发生什么? - Trajanus
@Trajanus,抱歉耽搁了。正如你所说,有一些新的配置部分,例如System.Web.Extensions没有在machine.config中定义。这些部分与ASP.NET AJAX服务相关。因此,如果您删除它们的定义,则无法使用它们。如果您不需要它们,请删除此部分。 - m3kh

0

您需要重新定义它以更改网站的行为。

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


0

所有的.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配置文件层次结构和继承


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接