欢迎光临
我们一直在努力

配置Config.web-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

asp.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>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 配置Config.web-.NET教程,Asp.Net开发
分享到: 更多 (0)

相关推荐

  • 暂无文章