web.config 文件查找规则:
(1)如果在当前页面所在目录下存在web.config文件,查看是否存在所要查找的结点名称,如果存在返回结果并停止查找。
(2)如果当前页面所在目录下不存在web.config文件或者web.config文件中不存在该结点名,则查找它的上级目录,直到网站的根目录。
(3)如果网站根目录下不存在web.config文件或者web.config文件中不存在该节点名则在%windir%"Microsoft.NET"Framework"v2.0.50727"CONFIG"web.config文件中查找。
(4) 如果在%windir%"Microsoft.NET"Framework"v2.0.50727"CONFIG"web.config文件中不存在相应 结点,则在%windir%"Microsoft.NET"Framework"v2.0.50727"CONFIG"machine.config文件 中查找。
(5)如果仍然没有找到则返回null。
在asp.net应用程序运行过程中,如果web.config文件发生更改就会导致相应的应用程序重新启动,这时存储在服务器内存中的用户会话信息 就会丢失(如存储在内存中的Session)。
(一)appSetings配置节点
<appSettings>节点主要用来存储asp.net应用程序的一些配置信息,比如上传文件的保存路径等
1
2
3
|
< appSettings > </ appSettings > |
1
|
string fileType=ConfigurationManager.AppSettings[ "FileType " ]; |
(二)<connectionStrings>节点
<connectionStrings> 节点主要用于配置数据库连接的,我们可以<connectionStrings>节点中增加任意个节点来保存数据库连接字符串,将来在代码中 通过代码的方式动态获取节点的值来实例化数据库连接对象,这样一旦部署的时候数据库连接信息发生变化我们仅需要更改此处的配置即可,而不必因为数据库连接 信息的变化而需要改动程序代码和重新部署
1
2
3
|
< connectionStrings > < add name = "AspNetStudyConnectionString1" connectionString = "Data Source=(local);Initial Catalog=AspNetStudy;User ID=sa;Password=sa" /> </ connectionString > |
1
|
string connectionString = ConfigurationManager.ConnectionStrings[ "AspNetStudyConnectionString1" ].ConnectionString; |
(三)<compilation>节点
<compilation>节点配置 ASP.NET 使用的所有编译设置。默认的debug属性为“true”,即允许调试,在这种情况下会影响网站的性能,所以在程序编译完成交付使用之后应将其设为“false”。
(四)<authentication>节点
设置asp.net身份验证模式,有四种身份验证模式,它们的值分别如下:
Windows 使用Windows身份验证,适用于域用户或者局域网用户。
Forms 使用表单验证,依靠网站开发人员进行身份验证。
Passport 使用微软提供的身份验证服务进行身份验证。
None 不进行任何身份验证。
(五)<customErrors>节点
<customErrors>节点用于定义 一些自定义错误信息的信息。此节点有Mode和defaultRedirect两个属性,其中defaultRedirect属性是一个可选属性,表示应 用程序发生错误时重定向到的默认URL,如果没有指定该属性则显示一般性错误。Mode属性是一个必选属性,它有三个可能值,它们所代表的意义分别如下:
On 表示在本地和远程用户都会看到自定义错误信息。
Off 禁用自定义错误信息,本地和远程用户都会看到详细的错误信息。
RemoteOnly 表示本地用户将看到详细错误信息,而远程用户将会看到自定义错误信息。
这 里有必要说明一下本地用户和远程用户的概念。当我们访问asp.net应用程时所使用的机器和发布asp.net应用程序所使用的机器为同一台机器时成为 本地用户,反之则称之为远程用户。在开发调试阶段为了便于查找错误Mode属性建议设置为Off,而在部署阶段应将Mode属性设置为On或者 RemoteOnly,以避免这些详细的错误信息暴露了程序代码细节从而引来黑客的入侵。
(六)<error>子节点
在<customErrors>节点下还包含有< error>子节点,这个节点主要是根据服务器的HTTP错误状态代码而重定向到我们自定义的错误页面,注意要使<error>子节点 下的配置生效,必须将<customErrors>节点节点的Mode属性设置为“On”。下面是一个例子:
1
2
3
4
|
< customErrors mode = "On" defaultRedirect = "GenericErrorPage.htm" > < error statusCode = "403" redirect = "403.htm" /> < error statusCode = "404" redirect = "404.htm" /> </ customErrors > |
(七)<httpHandlers>节点
<httpHandlers>节点用于根据用户请求的URL和HTTP谓词将用户的请求交给相应的处理程序。可以在配置级别的任何层次配置此节点,也就是说可以针对某个特定目录下指定的特殊文件进行特殊处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
< httpHandlers > < add path = "*.rules" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.xoml" verb = "*" type = "System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate = "false" /> < add path = "trace.axd" verb = "*" type = "System.Web.Handlers.TraceHandler" validate = "true" /> < add path = "WebResource.axd" verb = "GET" type = "System.Web.Handlers.AssemblyResourceLoader" validate = "true" /> < add path = "*.axd" verb = "*" type = "System.Web.HttpNotFoundHandler" validate = "true" /> < add path = "*.aspx" verb = "*" type = "System.Web.UI.PageHandlerFactory" validate = "true" /> < add path = "*.ashx" verb = "*" type = "System.Web.UI.SimpleHandlerFactory" validate = "true" /> < add path = "*.asmx" verb = "*" type = "System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate = "false" /> < add path = "*.rem" verb = "*" type = "System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate = "false" /> < add path = "*.soap" verb = "*" type = "System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate = "false" /> < add path = "*.asax" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.ascx" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.master" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.skin" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.browser" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.sitemap" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.dll.config" verb = "GET,HEAD" type = "System.Web.StaticFileHandler" validate = "true" /> < add path = "*.exe.config" verb = "GET,HEAD" type = "System.Web.StaticFileHandler" validate = "true" /> < add path = "*.config" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.cs" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.csproj" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.vb" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.vbproj" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.webinfo" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.licx" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.resx" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.resources" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.mdb" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.vjsproj" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.java" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.jsl" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.ldb" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.ad" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.dd" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.ldd" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.sd" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.cd" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.adprototype" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.lddprototype" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.sdm" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.sdmDocument" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.mdf" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.ldf" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.exclude" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.refresh" verb = "*" type = "System.Web.HttpForbiddenHandler" validate = "true" /> < add path = "*.svc" verb = "*" type = "System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate = "false" /> < add path = "*" verb = "GET,HEAD,POST" type = "System.Web.DefaultHttpHandler" validate = "true" /> < add path = "*" verb = "*" type = "System.Web.HttpMethodNotAllowedHandler" validate = "true" /> </ httpHandlers > |
从上面的配置中可以看出,针对*.mdf、*.ldf文件的Get或者Post请求都会交给 System.Web.HttpForbiddenHandler来处理,处理的结果就是用户不能查看或者下载相关的文件。如果我们某个文件夹下的文件或 者某个类型的文件不允许用户下载,可以在</httpHandlers>节点中增加相应的子节点。
下面我们以一个例子来说明<httpHandlers>节点的用法,在我们的asp.net应用程序中建立一个IPData目录,在IPData目录中创建一个IPData.txt文件,然后在Web.config中添加以下配置:
1
2
3
|
< httpHandlers > < add path = "IPData/*.txt" verb = "*" type = "System.Web.HttpForbiddenHandler" /> </ httpHandlers > |
(九)<httpRuntime>节点
<httpRuntime>节点用于对 ASP.NET HTTP 运行库设置。该节可以在计算机、站点、应用程序和子目录级别声明。
例如下面的配置控制用户最大能上传的文件为40M(40*1024K),最大超时时间为60秒,最大并发请求为100个
1
|
< httpRuntime maxRequestLength = "40960" executionTimeout = "60" appRequestQueueLimit = "100" /> |
(十)<pages>节点
<pages>节点用于表示对特定页设置,主要有三个属性,分别如下:
buffer 是否启用了 HTTP 响应缓冲。
enableViewStateMac 是否应该对页的视图状态运行计算机身份验证检查 (MAC),以放置用户篡改,默认为false,如果设置为true将会引起性能的降低。
validateRequest 是否验证用户输入中有跨站点脚本攻击和SQL注入式漏洞攻击,默认为true,如果出现匹配情况就会发HttpRequestValidationException 异常。对于包含有在线文本编辑器页面一般自行验证用户输入而将此属性设为false。
1
|
< pages buffer = "true" enableViewStateMac = "true" validateRequest = "false" /> |
(十一)<sessionState>节点
<sessionState>节点用于配置当前asp.net应用程序的会话状态配置。以下就是一个常见配置:
1
|
< sessionState cookieless = "false" mode = "InProc" timeout = "30" /> |
上面的节点配置是设置在asp.net应用程序中启用Cookie,并且指定会话状态模式为在进程中保存会话状态,同时还指定了会话超时为30分钟。
<sessionState>节点的Mode属性可以是以下几种值之一:
Custom 使用自定义数据来存储会话状态数据。
InProc 默认值。由asp.net辅助进程来存储会话状态数据。
Off 禁用会话状态。
SQLServer 使用进程外SQL Server数据库保存会话状态数据。
StateServer 使用进程外 ASP.NET 状态服务存储状态信息。
一般默认情况下使用InProc模式来存储会话状态数据,这种模式的好处是存取速度快,缺点是比较占用内存,所以不宜在这种模式下存储大型的用户会话数据
(十二)<globalization>节点
用于配置应用程序的全球化设置。此节点有几个比较重要的属性,分别如下:
fileEncoding 可选属性。设置.aspx、.asmx 和 .asax 文件的存储编码。
requestEncoding 可选属性。设置客户端请求的编码,默认为UTF-8.
responseEncoding 可选属性。设置服务器端响应的编码,默认为UTF-8.
以下就是asp.net应用程序中的默认配置:
1
|
< globalization fileEncoding = "utf-8" requestEncoding = "utf-8" responseEncoding = "utf-8" /> |
(十三)web.config文件读写
1
2
3
4
5
6
7
8
9
10
11
12
|
public void SetAppSetting( string key, string value) { AppSettingsSection appSetting = (AppSettingsSection)config.GetSection( "appSettings" ); if (appSetting.Settings[key] == null ) //如果不存在此节点,则添加 { appSetting.Settings.Add(key, value); } else //如果存在此节点,则修改 { appSetting.Settings[key].Value = value; } } |