整理如下:
配置文件的规则
ASP.NET的配置文件是基于XML格式的纯文本文件,存在于应用的各个目录下,统一命名为
“config.web”。它决定了所在目录及其子目录的配置信息,并且子目录下的配置信息覆盖
其父目录的配置。
WINNT/Microsoft.NET/Framework/版本号/下的config.web为整个机器的根配置文件,它定义
了整个环境下的缺省配置。
缺省情况下,浏览器是不能够直接访问目录下的config.web文件。
在运行状态下,ASP.NET会根据远程URL请求,把访问路径下的各个config.web配置文件叠加
,产生一个唯一的配置集合。举例来说,一个对URL: http://localhost/webapp/owndir/
test.aspx的访问,ASP.NET会根据以下顺序来决定最终的配置情况:
1../Microsoft.NET/Framework/v.1.00/config.web (缺省配置文件)
2../webapp/config.web (应用的配置)
3../webapp/owndir/config.web (自己的配置)
检查是否创建格式规范XML文档
CkeckWellFormed()
{
if (XMLsource.value != "")
xmlid.loadXML(Xmlsource.value);
if (xmlid.parseError.reason == "")
alter("Your XML is well formed");
else
alter(xmlid.parseEeror.reason) ;
}
else alter("Please enter an XML document.");
}
配置文件的语法规则
1) 标识
配置内容被置于config.web文件中的标记<configuration>和</configuration>之间。
格式:
<configuration>
配置内容
…
</configuration>
2)配置段句柄说明
ASP.NET的配置文件架构并未指定任何文件格式或者是支持的配置属性。相反的,它提出
了“配置段句柄申明”的概念来支持任意的用户定义配置段。
格式:
<configsections>
<add name=欲定义配置段名 type=处理的句柄程序 />
</configsections>
配置段
具体定义配置的内容,供应用使用。
以下例子定义了一个“httpmodules”配置段,设置了系统http相关的处理模块
<configuration>
<configsections>
<add name="httpmodules" type="System.Web.Configuration.HttpModules
ConfigurationHandler" />
</configsections>
<httpmodules>
<add type="System.Web.SessionState.CookielessSessionModule" />
<add type="System.Web.Caching.OutputCacheModule" />
<add type="System.Web.SessionState.SessionStateModule" />
<add type="System.Web.Security.WindowsAuthenticationModule" />
<add type="System.Web.Security.CookieAuthenticationModule" />
<add type="System.Web.Security.PassportAuthenticationModule" />
<add type="System.Web.Security.CustomAuthenticationModule" />
<add type="System.Web.Security.UrlAuthorizationModule" />
<add type="System.Web.Security.FileAuthorizationModule" />
</httpmodules>
</configuration>
ASP.NET定义的标准配置段
1) httpmodule 段: 配置应用程序内的http模块,http模块参与应用程序的每一个
请求,诸如安全、日志,身份验证之类的应用
以下示例添加对 ASP.NET 应用程序的三个 HttpModule 引用。
<configuration>
<system.web>
<httpModules>
<add type="System.Web.Caching.OutputCacheModule"
name="OutputCache"/>
<add type="System.Web.SessionState.SessionStateModule"
name="Session"/>
<add type=Selector, selector.dll"
name="Selector"/>
</httpModules>
</system.web>
</configuration>
2) httphandlers 段: http程序处理程序和处理机,是asp.net框架的骨干,负责把
输入的URL请求到IhttpHandler等类
以下示例将对文件扩展名为 .New 的文件的所有 HTTP 请求映射到类 MyHandler.New,将对
文件扩展名为 .MyNewFileExtension 的文件的 HTTP GET 和 HTTP HEAD 请求映射到类
MyHandler.MNFEHandler。这两个类都在文件 MyHandler.dll 中的程序集 MyHandler 中。
<configuration>
<system.web>
<httpHandlers>
<add verb="*"
path="*.New"
type="MyHandler.New,MyHandler"/>
<add verb="GET,HEAD"
path="*.MyNewFileExtension"
type="MyHandler.MNFEHandler,MyHandler.dll"/>
</httpHandlers>
<system.web>
</configuration>
3) sessionstat 段: 负责配置http模块的会话状态
参考微软:http://msdn.microsoft.com/library/chs/default.asp?
url=/library/CHS/cpgenref/html/gngrfhttphandlerssection.asp
<sessionState mode="Off|InProc|StateServer|SQLServer"
cookieless="true|false"
timeout="number of minutes"
stateConnectionString="tcpip=server:port"
sqlConnectionString="sql connection string"
stateNetworkTimeout="number of seconds"/>
4) globalization 段: 配置全局环境
<globalization requestEncoding="any valid encoding string"
responseEncoding="any valid encoding string"
fileEncoding="any valid encoding string"
culture="any valid culture string"
uiCulture="any valid culture string"/>
以下示例为 ASP.NET 应用程序指定默认请求和响应编码。
<configuration>
<system.web>
<globalization
requestEncoding="iso-8859-1"
responseEncoding="iso-8859-1"/>
</system.web>
</configuration>注意:在页面可以覆盖配置设置,<%@page Culture="fr" UICulture="fr"
ResponseEncoding="ute-8"%>
5) compilation 段: 配置ASP.NET的载入哪个程序集, 程序集存储在/bin目录
下.,程序集是使用c#,vb.net,javascrip编译器编译的业务组件.业务组件是可以在web窗体中
运行的类.
<complation>标记本身的属性指定编译选项(属性参
考:http://msdn.microsoft.com/library/chs/default.asp?
url=/library/CHS/cpgenref/html/gngrfassemblies.asp),把写好的类编译到/bin下,如在c#
环境下则用:csc /t:library /out ../../../bin/HelloObj.dll HelloObj.vb 那么在应用程
序中可以使用该组件
<%@ Import Namespace = "HelloWorlder" %>
以下示例添加对应用程序的程序集引用。
<configuration>
<compilation>
<assemblies>
<add assembly="System.Data />
</assemblies>
</compilation>
</configuration>6) trace 段: 配置ASP.NET的跟踪服务
在asp.net中提供两种级别的跟踪:页级和应用程序级.要更加详细了解跟踪服务,请参阅国人
翻译的文章:
http://truly.cnblogs.com/archive/2006/05/13/398861.html
以下示例指定跟踪配置设置。
<configuration>
<system.web>
<trace enabled="false"
pageOutput="true"
requestLimit="15"/>
<system.web>
</configuration>
7) security 段: ASP.NET的安全配置
8) iisprocessmodel 段: 在IIS上配置ASP.NET的处理模式
9) browercaps 段: 配置浏览器的兼容部件
一个配置读出的例子
1) 在config.web配置文件中自定义一个配置节
<!--config.web 请放入FormCfg.aspx所在目录-->
<configuration>
<!--申明一个test配置段-->
<configsections>
<add name="test" type="System.Web.Configuration.DictionarySectionHandler"
/>
</configsections>
<test>
<!--配置一个键key,其内容为just a configure test-->
<add key="key" value="just a configure test" />
</test>
</configuration>
2) 读出其内容
<!--文件名:Application/FormCfg.aspx-->
<html>
<head>
<script language="VB" runat=server>
sub page_load(s as object ,e as eventargs)
'取出test配置段的key键的值
Dim CfgSection As Hashtable = Context.GetConfig("test")
Dim Msg As String = CStr(CfgSection("key"))
lblMsg.text=Msg
end sub
</script>
<title>
配置信息的读取
</title>
</head>
<body>
<center>
config.web中"test"配置段中key的内容为:
<asp:label id=lblmsg runat=server />
</center>
</body>
</html>
3) 运行结果
4.2. 6 Config.web配置实例
<configuration>
<!--定义用户应用的公用设置,如SQL的sql连接串等等-->
<appsettings>
</appsettings>
<!--设置浏览器的兼容性部件-->
<browsercaps>
</browsercaps>
<!--编译环境设置,非调试模式-->
<compilation debugmode="false">
<!--缺省编译语言为vb,以后可以不再在Page中定义脚本语言-->
<compilers defaultlanguage="vb">
<!--以MSVSA.dll编译.vb为后缀的VB文件-->
<compiler language="VB" extension=".vb" type="MSVSA.dll#Microsoft.VB.Compiler"/>
</compilers>
<assemblies>
<!--加入对System.Data的引用-->
<add assembly="System.Data" />
<!--去掉对System.Data的引用-->
<remove assembly="System.IO" />
<!--去掉config.web中包含或继承来的引用-->
<clear />
</assemblies>
</compilation>
<!--设置应用全局环境-->
<!--文件、请求、返回以gb2312编码,以保证浏览器正确显示中文-->
<globalization fileencoding="gb2312" requestencoding="gb2312"
responseencoding="gb2312"/>
<!--定义用户出错的处理-->
<!--出错缺省显示defaultredirect指定的页面,mode为on时,遵循customerrors配置段-->
<!--mode为off时,忽略用户出错,mode为remoteonly时,本地才显示真正的出错原因-->
<customerrors defaultredirect="AnErrorHasOccured.aspx?ErrNum=-1" mode="remote">
<!--当出错码为500时,显示redirect指定的页面-->
<error statuscode="500" redirect="AnErrorHasOccured.aspx?ErrNum=500"/>
</customerrors>
<!--指定目录webapp的访问权限-->
<location path="webapp” >
<!--非授权用户不能进入webapp目录-->
<security>
<authorization>
<deny users="?" />
</authorization>
</security>
</location>
<!--定义安全属性-->
<security>
<authorization>
<!--角色为Adminstrators和所有的用户访问其指定的资源-->
<allow roles="Adminstrators"/>
<allow users="*" />
</authorization>
</security>
</configuration>
在页级使用Culture和RegionInfo类提供使用那一个地区的语言设置
culture = New CultureInfo(NewCulture.Value);
region = New Region(NewRegion.Value)