ASP.NET应用程序的行为受配置文件中不同设置的影响:
machine.config
web.config
machine.config文件包含所有支持设置的默认值和机器特定值.机器设置由系统管理员控制,应用程序通常无法访问此文件.
然而,应用程序可以通过在其根文件夹中创建web.config文件来覆盖默认值. web.config文件是machine.config文件的子集.
如果应用程序包含子目录,则可以为每个文件夹定义web.config文件.每个配置文件的范围都是以自上而下的分层方式确定的.
任何web.config文件都可以在本地扩展,限制或覆盖在上一级定义的任何设置.
Visual Studio为每个项目生成一个默认的web.config文件.应用程序可以在没有web.config文件的情况下执行,但是,如果没有web.config文件,则无法调试应用程序.
下图显示了解决方案资源管理器中使用的示例示例Web服务教程:
在此应用程序,有两个web.config文件用于两个项目,即Web服务和调用Web服务的Web站点.
web.config文件将配置元素作为根节点.此元素内的信息分为两个主要区域:配置节 - 处理程序声明区和配置节设置区.
以下代码段显示配置文件的基本语法:
<configuration> <!-- Configuration section-handler declaration area. --> <configSections> <section name="section1" type="section1Handler" /> <section name="section2" type="section2Handler" /> </configSections> <!-- Configuration section settings area. --> <section1> <s1Setting1 attribute1="attr1" /> </section1> <section2> <s2Setting1 attribute1="attr1" /> </section2> <system.web> <authentication mode="Windows" /> </system.web> </configuration>
配置节处理程序包含在< configSections>中.标签.每个配置处理程序指定包含在文件中的配置节的名称,该节提供一些配置数据.它具有以下基本语法:
<configSections> <section /> <sectionGroup /> <remove /> <clear/> </configSections>
它有以下元素:
清除 - 删除对继承的部分和部分组的所有引用.
删除 - 删除引用继承的节和节组.
节 - 它定义配置节处理程序和配置元素之间的关联.
部分组 - 它定义了配置部分处理程序和配置部分之间的关联.
应用程序设置允许存储应用程序范围的名称 - 值对以进行只读访问.例如,您可以将自定义应用程序设置定义为:
<configuration> <appSettings> <add key="Application Name" value="MyApplication" /> </appSettings> </configuration>
例如,您还可以存储图书的名称及其ISBN编号:
<configuration> <appSettings> <add key="appISBN" value="0-273-68726-3" /> <add key="appBook" value="Corporate Finance" /> </appSettings> </configuration>
连接字符串显示网站可用的数据库连接字符串.例如:
<connectionStrings> <add name="ASPDotNetStepByStepConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\\projects\datacaching\ / datacaching\App_Data\ASPDotNetStepByStep.mdb" providerName="System.Data.OleDb" /> <add name="booksConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\ \databinding\App_Data\books.mdb" providerName="System.Data.OleDb" /> </connectionStrings>
system.web元素指定ASP.NET配置部分的根元素和包含配置ASP.NET Web应用程序并控制应用程序行为方式的配置元素.
它包含在常见应用程序中需要调整的大多数配置元素.该元素的基本语法如下:
<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>
下表提供了 system.web 元素的一些常见子元素的简要说明:
这是识别需要授权时未经过身份验证的用户所必需的.
它配置身份验证支持.基本语法如下:
<authentication mode="[Windows|Forms|Passport|None]"> <forms>...</forms> <passport/> </authentication>
它配置授权支持.基本语法如下:
<authorization> <allow .../> <deny .../> </authorization>
It配置缓存设置.基本语法如下:
<caching> <cache>...</cache> <outputCache>...</outputCache> <outputCacheSettings>...</outputCacheSettings> <sqlCacheDependency>...</sqlCacheDependency> </caching>
它定义了自定义错误消息.基本语法如下:
<customErrors defaultRedirect="url" mode="On|Off|RemoteOnly"> <error. . ./> </customErrors>
它定义了用于部署的配置设置.基本语法如下:
<deployment retail="true|false" />
它定义了托管环境的配置设置.基本语法如下:
<hostingEnvironment idleTimeout="HH:MM:SS" shadowCopyBinAssemblies="true|false" shutdownTimeout="number" urlMetadataSlidingExpiration="HH:MM:SS" />
它配置应用程序的身份.基本语法如下:
<identity impersonate="true|false" userName="domain\username" password="<secure password>"/>
它配置用于加密和解密Forms身份验证cookie数据的密钥.
它还允许配置验证密钥,该验证密钥对视图状态数据执行消息验证检查并形成验证票证.基本语法是:
<machineKey validationKey="AutoGenerate,IsolateApps" [String] decryptionKey="AutoGenerate,IsolateApps" [String] validation="HMACSHA256" [SHA1 | MD5 | 3DES | AES | HMACSHA256 | HMACSHA384 | HMACSHA512 | alg:algorithm_name] decryption="Auto" [Auto | DES | 3DES | AES | alg:algorithm_name] />
这会配置管理和验证用户帐户的参数.基本语法是:
<membership defaultProvider="provider name" userIsOnlineTimeWindow="number of minutes" hashAlgorithmType="SHA1"> <providers>...</providers> </membership>
它提供特定于页面的配置.基本语法是:
<pages asyncTimeout="number" autoEventWireup="[True|False]" buffer="[True|False]" clientIDMode="[AutoID|Predictable|Static]" compilationMode="[Always|Auto|Never]" controlRenderingCompatibilityVersion="[3.5|4.0]" enableEventValidation="[True|False]" enableSessionState="[True|False|ReadOnly]" enableViewState="[True|False]" enableViewStateMac="[True|False]" maintainScrollPositionOnPostBack="[True|False]" masterPageFile="file path" maxPageStateFieldLength="number" pageBaseType="typename, assembly" pageParserFilterType="string" smartNavigation="[True|False]" styleSheetTheme="string" theme="string" userControlBaseType="typename" validateRequest="[True|False]" viewStateEncryptionMode="[Always|Auto|Never]" > <controls>...</controls> <namespaces>...</namespaces> <tagMapping>...</tagMapping> <ignoreDeviceFilters>...</ignoreDeviceFilters> </pages>
它配置用户配置文件参数.基本语法是:
<profile enabled="true|false" inherits="fully qualified type reference" automaticSaveEnabled="true|false" defaultProvider="provider name"> <properties>...</properties> <providers>...</providers> </profile>
它配置用户角色的设置.基本语法是:
<roleManager cacheRolesInCookie="true|false" cookieName="name" cookiePath="/" cookieProtection="All|Encryption|Validation|None" cookieRequireSSL="true|false " cookieSlidingExpiration="true|false " cookieTimeout="number of minutes" createPersistentCookie="true|false" defaultProvider="provider name" domain="cookie domain"> enabled="true|false" maxCachedResults="maximum number of role names cached" <providers>...</providers> </roleManager>
它配置安全策略.基本语法是:
<securityPolicy> <trustLevel /> </securityPolicy>
它定义了映射以隐藏原始URL并提供更友好的用户界面.基本语法是:
<urlMappings enabled="true|false"> <add.../> <clear /> <remove.../> </urlMappings>
它提供客户端脚本的共享位置名称.基本语法是:
<webControls clientScriptsLocation="String" />
这会配置网络服务.