您的位置:首页 > 编程语言 > ASP

ASP.NET Forum 学习笔记系列--Web.config

2007-01-12 16:59 603 查看
预备知识:
 
 
 
所有配置信息都驻留在 <configuration> 和 </configuration> 根 XML 标记之间。标记间的配置信息分为两个主区域:配置节处理程序声明区域和配置节设置区域。
 
配置节处理程序声明出现在配置文件顶部 <configSections> 和 </configSections> 标记之间。
 
 
 
<location>元素
 
指定应用配置设置的资源。
 
 
 
原型:
 
<locationpath="path" allowOverride="true|false"/>
 
 
 
可选属性
 
path 应用指定配置设置的资源。使用缺少路径属性的 <location> 将配置设置应用于当前目录及其所有子目录。
 
allowOverride 指定配置设置是否可以被子目录中的web.config 文件的配置设置重写。
 
 
 
<system.web>元素
 
为 ASP.NET 配置节指定根元素。包含各种配置元素,这些配置元素配置 ASP.NET Web 应用程序并控制这些应用程序的行为方式。
 
 
 
<system.web>
 
   <authentication>
 
   <authorization> 
 
   <browserCaps>  
 
   <clientTarget>
 
   <compilation>
 
   <customErrors>
 
   <globalization>
 
   <httpHandlers>
 
   <httpModules>
 
   <httpRuntime>
 
   <identity>
 
   <machineKey>
 
   <pages>
 
   <processModel>
 
   <securityPolicy>
 
   <serviceDescriptionFormatExtensionTypes>
 
   <sessionState>
 
   <trace>
 
   <trust>
 
   <webServices>
 
</system.web>
 
 
 
把几个常用的以及今天要用到的元素描述一下:
 
 
 
<authentication>元素
 
 
 
配置 ASP.NET 身份验证支持。
 
 
 
<authenticationmode="Windows|Forms|Passport|None">
 
   <formsname="name" //指定要用于身份验证的HTTPCookie。
 
          loginUrl="url"      //如果没找到任何有效的身份验证Cookie,将请求重定向到的URL。
 
          protection="All|None|Encryption|Validation"    //指定Cookie使用的加密类型。
 
          timeout="30"path="/"     //Cookie过期时间
 
          requireSSL="true|false"     //指定是否需要安全连接来转换身份验证Cookie
 
          slidingExpiration="true|false"> //指定是否启用弹性过期时间
 
   </forms>
 
   <passportredirectUrl="internal"/> //指定要重定向到的页(如没有登录,则转到注册页. 如:login.asp)
 
</authentication>
 
 
 
<authorization>元素
 
配置 ASP.NET 授权支持。
 
 
 
<authorization>
 
   <allowusers="×,×,×"   //逗号分隔的具有访问权限的用户名列表。问号(?)允许匿名用户;星号(*)允许所有用户
 
          roles="comma-separated list of roles"
 
          verbs="×,×,×"/> //逗号分隔的 HTTP 传输方法列表,这些 HTTP 传输方法被允许对资源的访问。注册到 ASP.NET 的谓词为 GET、HEAD、POST 和 DEBUG。
 
 
 
   <denyusers="×,×,×" //逗号分隔的拒绝访问的用户名列表。问号(?)拒绝匿名用户;星号(*)拒绝所有用户
 
         roles="comma-separated list of roles"
 
         verbs="comma-separated list of verbs"/>
 
</authorization>
 
 
 
<customErrorsdefaultRedirect="url" 
 
                    mode="On|Off|RemoteOnly"> //根据mode的配置可以决定是否显示自定义错误信息
 
   <errorstatusCode="statuscode" //指定将导致重定向到错误页的HTTP状态代码。
 
   redirect="url"/> //将向客户端展示有关该错误的信息的错误页。
 
</customErrors>
 
 
 
<httpHandlers>元素
 
 
 
<httpHandlers>
 
   <addverb="verb list" //谓词列表可以是逗号分隔的HTTP谓词列表(例如“GET,PUT,POST”),也可以是开始脚本映射(例如通配符*[星号])。
 
        path="path/wildcard" //路径属性可以包含单个URL路径或简单的通配符字符串(例如*.aspx)。
 
        type="type,assemblyname" //逗号分隔的类/程序集组合。ASP.NET首先在应用程序的专用 /bin目录中搜索程序集DLL,然后在系统程序集缓存中搜索程序集DLL。
 
        validate="true|false"/>
 
   <removeverb="verb list" //与要移除的处理程序的谓词列表相匹配的谓词列表。
 
           path="path/wildcard"/> //与要移除的处理程序的路径相匹配的路径。
 
   <clear/>
 
</httpHandlers>
 
 
 
 
 
 
 
以下示例将对文件扩展名为 .New 的文件的所有 HTTP 请求映射到类 MyHandler.New,将对文件扩展名为 .MyNewFileExtension 的文件的 HTTP GET 和 HTTP HEAD 请求映射到类 MyHandler.MNFEHandler。这两个类都在文件 MyHandler.dll 中的程序集 MyHandler 中。
 
 
 
<httpHandlers>
 
<configuration>
 
   <system.web>
 
      <httpHandlers>
 
         <addverb="*"
 
              path="*.New"
 
              type="MyHandler.New,MyHandler"/>
 
         <addverb="GET,HEAD"
 
              path="*.MyNewFileExtension"
 
              type="MyHandler.MNFEHandler,MyHandler.dll"/>
 
     </httpHandlers>
 
   <system.web>
 
</configuration>
 
 
 
 
 
<httpModules>元素
 
在一个应用程序内配置 HTTP 模块。
 
 
 
<httpModules>
 
   <addtype="classname,assemblyname" //指定由版本、程序集和公钥标记组成的逗号分隔的类/程序集组合。ASP.NET首先在应用程序的专用 /bin目录中搜索程序集DLL,然后在系统程序集缓存中搜索程序集DLL。
 
           name="modulename"/>             //模块名
 
   <removename="modulename"/>
 
   <clear/>
 
</httpModules>
 
 
 
<pages>元素
 
标识特定于页的配置设置。<pages> 节可以在计算机、站点、应用程序和子目录级别声明。
 
 
 
<pagesbuffer="true|false"                              //是否启用缓冲
 
       enableSessionState="true|false|ReadOnly" //是否启用会话状态
 
       enableViewState="true|false"                          //是否启用视图状态
 
       enableViewStateMac="true|false"                  //指定在从客户端回发页时,ASP.NET是否应该对页的视图状态运行消息身份验证代码(MAC)
 
        smartNavigation="true|false"                         //是否启用智能导航
 
       pageBaseType="typename, assembly"       //指定默认情况下.aspx页继承的代码隐藏类。
 
       userControlBaseType="typename"               //指定默认情况下用户控件继承的代码隐藏类。
 
       autoEventWireup="true/false"                          //是否自动启用页事件
 
       validateRequest="true|False"/>                   //指示 ASP.NET 在从浏览器输入的所有内容中检查是否存在潜在的危险数据
 
 
 
 
 
===========================================================
 
 
 
<configuration>
 
     <configSections>
 
 
 
         <!-- 初始化Asp.Net Forums Handler -->
 
         <sectionGroupname="forums">
 
              <sectionname="forums"type="AspNetForums.Configuration.ForumsConfigurationHandler, AspNetForums.Components"/>
 
         </sectionGroup>
 
 
 
     </configSections>
 
     <system.web>
 
 
 
         <!-- 标准应用程序设置 -->
 
         <compilationdefaultLanguage="c#"debug="true"/>
 
         <pagesvalidateRequest="false"autoEventWireup="true"pageBaseType="AspNetForums.Components.ForumPage, AspNetForums.Components"/>
 
 
 
         <!-- 远程客户端是否显示详细错误信息 -->
 
         <customErrorsmode="Off"/>
 
 
 
 
 
         <!-- 指定应用程序HttpModule -->
 
         <httpModules>
 
              <addname="AspNetForums"type="AspNetForums.ForumsHttpModule, AspNetForums.Components"/>
 
         </httpModules>
 
         <!-- 指定应用程序HttpHandlers -->
 
         <httpHandlers>
 
              <addverb="GET"path="avatar.aspx"type="AspNetForums.Components.HttpHandler.AvatarHttpHandler, AspNetForums.Components"/>
 
              <addverb="GET"path="vcard.aspx"type="AspNetForums.Components.HttpHandler.VCardHttpHandler, AspNetForums.Components"/>
 
         </httpHandlers>
 
         <authenticationmode="Forms">
 
              <formsname=".AspNetForums"protection="All"timeout="60"loginUrl="login.aspx"/>
 
         </authentication>
 
     </system.web>
 
     <!-- Asp.Net Forums 应用程序配置 -->
 
     <forums>
 
    
 
         <forums
 
              defaultProvider="SqlForumsProvider"      
 
              defaultLanguage="zh-CN"
 
              forumFilesPath="/"
 
              disableEmail="false"
 
              disableIndexing="true"
 
              disableThreading="false"
 
              threadIntervalStats="15"
 
              threadIntervalEmail="15"
 
              passwordEncodingFormat="unicode"
 
              smtpServerConnectionLimit="-1"
 
              enableLatestVersionCheck="false"
 
              uploadFilesPath="/Upload/"
 
         >
 
 
 
              <providers>
 
 
 
                   <clear/>
 
                  
 
                   <add
 
                       name="SqlForumsProvider"
 
                       type="AspNetForums.Data.SqlDataProvider, AspNetForums.SqlDataProvider"
 
                       connectionString="server=kokey.;database=Forums;uid=sa;pwd=sa"
 
                       databaseOwner="dbo"
 
                   />
 
 
 
              </providers>
 
 
 
                        <extensionModules>
 
                   <addname="PassportAuthentication"
 
                       extensionType="Security"
 
                       type="Telligent.CommunityServer.Security.PassportAuthentication, Telligent.CommunityServer.SecurityModules"
 
                   />
 
                   <addname="WindowsAuthentication"
 
                       extensionType="Security"
 
                       type="Telligent.CommunityServer.Security.WindowsAuthentication, Telligent.CommunityServer.SecurityModules"
 
                       allowAutoUserRegistration="false"
 
                       adminWindowsGroup="Administrators"
 
                       assignLocalAdminsAdminRole="false"
 
                   />
 
                        </extensionModules>
 
         </forums>
 
     </forums>
 
     <!-- END - AspNetForums configuration settings -->
 
    
 
     <!-- Asp.Net Forums 访问权限设置 -->
 
     <!—这里是对于特别的几个文件的权限设置,注意”?” 是通配符,表示匿名访问à
 
     <locationpath="EditPost.aspx">
 
         <system.web>
 
              <authorization>
 
                   <denyusers="?"/>
 
              </authorization>
 
         </system.web>
 
     </location>
 
 
 
     <locationpath="PostAttachmentManager.aspx">
 
         <system.web>
 
              <authorization>
 
                   <denyusers="?"/>
 
              </authorization>
 
         </system.web>
 
     </location>
 
 
 
     <locationpath="PrivateMessage.aspx">
 
         <system.web>
 
              <authorization>
 
                   <denyusers="?"/>
 
              </authorization>
 
         </system.web>
 
     </location>
 
    
 
     <locationpath="Download.aspx">
 
         <system.web>
 
              <authorization>
 
                   <denyusers="?"/>
 
              </authorization>
 
         </system.web>
 
     </location>
 
 
 
     <locationpath="License.aspx">
 
         <system.web>
 
              <authorization>
 
                   <denyusers="?"/>
 
              </authorization>
 
         </system.web>
 
     </location>
 
 
 
 
 
</configuration>
 
 
 

 
 
 

 

 
至此,所有的Web.config都已经被配置好了。现在开始在程序中引用Web.config中的节点。

 
在ForumConfiguration类里是这样处理的。

 
首先

 
public class ForumConfiguration {

 
… …
 
    public static ForumConfiguration GetConfig() {
 
    return (ForumConfiguration) ConfigurationSettings.GetConfig("forums/forums");
 
}
 
//注意这里用到了ConfigurationSettings sealed类的 GetConfig() 方法,它是用来返回用户自定义的配置节的配置设置的方法。
 
public static object GetConfig( string sectionName );//sectionName 要读取的配置节。
 
 
 
 
 
 
 
// 实现IconfigrationSectionHandler接口,此接口只有一个方法Create,用来分析配置节的 XML。返回的对象被添加到配置集合中,并通过 GetConfig 访问。
 
    internal class ForumsConfigurationHandler : IConfigurationSectionHandler {
 
 
 
        public virtual object Create(Object parent, Object context, XmlNode node) {
 
            ForumConfiguration config = new ForumConfiguration();
 
            config.LoadValuesFromConfigurationXml(node);
 
            return config;
 
        }
 
}
 
 
 
好了,今天对于Web.config的学习到此结束。
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息