Machine.config 檔包含伺服器上所有 Web 應用程式的 ASP.NET 結構描述,web.config從它繼承而來.
web.config文件中只包含一個根節點<configuration>,其它节點都包含在它的内部.其中常用的有以下5個節點:<system.web>,<connectionStrings>,<location>,<appSettings>,<configSections>下面分別對這些節點作一介紹:
1.<system.web>
作用:控制Asp.net运行时的行为
包含元素如下:
<system.web>
<anonymousIdentification>
<authentication>
<authorization>
<browserCaps>
<caching>
<clientTarget>
<compilation>
<customErrors>
<deployment>
<deviceFilters>
<globalization>
<healthMonitoring>
<hostingEnvironment>
<httpCookies>
<httpHandlers>
<httpModules>
<httpRuntime>
<identity>
<machineKey>
<membership>
<mobileControls>
<pages>
<processModel>
<profile>
<roleManager>
<securityPolicy>
<sessionPageState>
<sessionState>
<siteMap>
<trace>
<trust>
<urlMappings>
<webControls>
<webParts>
<webServices>
<xhtmlConformance>
</system.web>
下面只對其中部分常用的元素做一說明:
--1.<authentication>
作用:配置 ASP.NET 身份验证支持(为Windows、Forms、PassPort、None四种)。该元素只能在计算机、站点或应用程序级别声明。<authentication> 元素必需与<authorization> 节配合使用。
<authentication mode="Forms" >
<forms loginUrl="logon.aspx" name=".FormsAuthCookie"/>
</authentication>
--2.<authorization>
作用:控制对 URL 资源的客户端访问(如允许匿名用户访问)。此元素可以在任何级别(计算机、站点、应用程序、子目录或页)上声明。必需与<authentication> 节配合使用。
<authorization>
<deny users="?"/>
</authorization>
--3.<compilation>
作用:配置 ASP.NET 使用的所有编译设置。默认的debug属性为“True”.在程序编译完成交付使用之后应将其设为False以提高性能.
<compilation debug="true"/>
--4.<customErrors>
作用:为 ASP.NET 应用程序提供有关自定义错误信息的信息。它不适用于 XML Web services 中发生的错误。
<customErrors defaultRedirect="ErrorPage.aspx" mode="RemoteOnly">
</customErrors>
--5.<httpRuntime>
作用:配置 ASP.NET HTTP 运行库设置。该节可以在计算机、站点、应用程序和子目录级别声明。
--6. <pages>
作用:标识特定于页的配置设置(如是否启用会话状态、视图状态,是否检测用户的输入等)。<pages>可以在计算机、站点、应用程序和子目录级别声明。
<pages buffer="true" enableViewStateMac="true" validateRequest="false"/>
--7.<sessionState>
作用:为当前应用程序配置会话状态设置(如设置是否启用会话状态,会话状态保存位置)。
<sessionState mode="InProc" cookieless="true" timeout="20"/>
</sessionState>
--8.<trace>
作用:配置 ASP.NET 跟踪服务,主要用来程序测试判断哪里出错。
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />
2.<connectionStrings>
作用:配置數據庫連接字符串
<connectionStrings>
<add name="Sales"
providerName="System.Data.SqlClient"
connectionString= "server=myserver;database=Products;uid=salesUser;pwd=sellMoreProducts" />
<add name="NorthWind"
providerName="System.Data.SqlClient"
connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />
</connectionStrings>
3.<location>
作用:指定应用子配置设置的资源。此元素也锁定配置设置,以防止子配置文件重写这些设置
格式: <location allowOverride="True|False" path="path" />
一個完整的示例:
<location path="Logon.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
4.<appSettings>
作用:定义应用程序的全局常量设置等信息.
<appSettings>
<add key="Application1" value="MyApplication1" />
<add key="Setting1" value="MySetting" />
</appSettings>
5.<configSections>
作用:指定配置节和命名空间声明,位于配置文件的顶部,如果要在子配置文件web.config中自定義新的結點,以及要對繼承的結點做一些配置或移除繼承的某個節點,也可以使用該方法做配置.
包含元素如下:
<configSections>
<section />
<sectionGroup />
<remove />
<clear/>
</configSections>
自定義配置節點示例,它包含兩部分,先是聲明,然後是定義:
<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<sampleSection setting1="Value1"
setting2="value two"
setting3="third value" />
</configuration>
參考文獻: 1. http://msdn2.microsoft.com/zh-tw/library/ms178685(VS.80).aspx
2. http://msdn2.microsoft.com/zh-cn/library/dayb112d(VS.80).aspx