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站点。
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
元素的一些常见子元素的简要描述:
anonymousidentification
这需要识别在需要授权时未通过身份验证的用户。
authentication
它用于配置身份验证支持。 基本的语法如下:
<authentication mode="[windows|forms|passport|none]">
<forms>...</forms>
<passport/>
</authentication>
authorization
它配置授权支持。基本的语法如下:
<authorization>
<allow .../>
<deny .../>
</authorization>
caching
它配置缓存设置。基本的语法如下:
<caching>
<cache>...</cache>
<outputcache>...</outputcache>
<outputcachesettings>...</outputcachesettings>
<sqlcachedependency>...</sqlcachedependency>
</caching>
customerrors
它定义了自定义错误消息。 基本的语法如下:
<customerrors defaultredirect="url" mode="on|off|remoteonly">
<error. . ./>
</customerrors>
deployment
它定义了用于部署的配置设置。 基本语法如下:
<deployment retail="true|false" />
hostingenvironment
它定义了托管环境的配置设置。 基本语法如下:
<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>"/>
machinekey
它配置密钥以用于表单身份验证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
这配置管理和认证用户帐户的参数。 基本的语法是:
<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
它配置用户角色的设置。 基本的语法是:
<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
它配置安全策略。 基本的语法是:
<securitypolicy>
<trustlevel />
</securitypolicy>
urlmappings
它定义了映射来隐藏原始url并提供更加用户友好的url。 基本的语法是:
<urlmappings enabled="true|false">
<add.../>
<clear />
<remove.../>
</urlmappings>
webcontrols
它提供客户端脚本的共享位置的名称。 基本的语法是:
<webcontrols clientscriptslocation="string" />
webservices
这用于配置web服务。