ASP.NET 配置
ASP.NET 应用程序的行为受配置文件中不同设置的影响:
- 机器配置
- 网络配置
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.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> <section /> <sectionGroup /> <remove /> <clear/> </configSections>
它具有以下要素:
-
Clear - 它删除对继承的节和节组的所有引用。
-
Remove - 它删除对继承的节和节组的引用。
-
Section - 它定义了配置节处理程序和配置元素之间的关联。
-
节组 - 它定义了配置节处理程序和配置节之间的关联。
应用程序设置
应用程序设置允许存储应用程序范围的名称-值对以进行只读访问。例如,你可以将自定义应用程序设置定义为:
<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="ASPDotNet步骤By步骤ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\\projects\datacaching\ / datacaching\App_Data\ASPDotNet步骤By步骤.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 元素
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>
下表提供了一些常见子元素的简要说明 系统网站 element:
匿名识别
这是在需要授权时识别未通过身份验证的用户所必需的。
验证
它配置身份验证支持。基本语法如下:
<authentication mode="[Windows|Forms|Passport|None]"> <forms>...</forms> <passport/> </authentication>
授权
它配置授权支持。基本语法如下:
<authorization> <allow .../> <deny .../> </authorization>
Caching
它配置缓存设置。基本语法如下:
<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
它配置应用程序的身份。基本语法如下:
<identity impersonate="true|false" userName="domain\username" password="<secure password>"/>
机器钥匙
它配置用于加密和解密表单身份验证 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
它提供特定于页面的配置。基本语法是:
<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
它配置用户配置文件参数。基本语法是:
<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 并提供更用户友好的 URL。基本语法是:
<urlMappings enabled="true|false"> <add.../> <clear /> <remove.../> </urlMappings>
网页控件
它为客户端脚本提供共享位置的名称。基本语法是:
<webControls clientScriptsLocation="String" />
网页服务
这将配置 Web 服务。