配置Config.web

80酷酷网    80kuku.com

  webASP.NET提供了一个丰富而可行的配置系统,以帮助管理人员轻松快速的建立自己的WEB应用环境。ASP.NET提供的是一个层次配置架构,可以帮助WEB应用、站点、机器分别配置自己的扩展配置数据。ASP.NET的配置系统具有以下优点:
●    ASP.NET允许配置内容可以和静态内容、动态页面和商业对象放置在同一应用的目录结构下。当管理人员需要安装新的ASP.NET应用时,只需要将应用目录拷贝到新的机器上即可。
●    ASP.NET的配置内容以纯文本方式保存,可以以任意标准的文本编辑器、XML解析器和脚本语言解释、修改配置内容。
●    ASP.NET 提供了扩展配置内容的架构,以支持第三方开发者配置自己的内容。
●    ASP.NET配置文件的更修被系统自动监控,无须管理人员手工干预。

4.2.2    配置文件的规则
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                   (自己的配置)
4.2.3    配置文件的语法规则
1)    标识
配置内容被置于config.web文件中的标记<configuration>和</configuration>之间。
格式:
<configuration>
    配置内容

    </configuration>

    2)配置段句柄说明
    ASP.NET的配置文件架构并未指定任何文件格式或者是支持的配置属性。相反的,它提出了“配置段句柄申明”的概念来支持任意的用户定义配置段。
    格式:
    <configsections>
        <add name=欲定义配置段名 type=处理的句柄函数 />
    </configsections>
    
3)    配置段
具体定义配置的内容,供应用使用。

以下例子定义了一个“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>

4.2. 4    ASP.NET定义的标准配置段
1)    httpmodule    段:  定义了应用的http请求的处理模块以及诸如安全、日志之类的应用方式
2)    httphandlers    段: 负责映射URLs到IhttpHandler类
3)    sessionstat     段:  负责配置http模块的会话状态
4)    globalization   段:  配置应用的公用设置
5)    compilation    段:  配置ASP.NET的编译环境
6)    trace       段:  配置ASP.NET的跟踪服务
7)    security        段: ASP.NET的安全配置
8)    iisprocessmodel 段: 在IIS上配置ASP.NET的处理模式
9)    browercaps     段: 配置浏览器的兼容部件
4.2. 5    一个配置读出的例子
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>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: