Asp.net Forums 论坛web.config文件详解

来源:互联网 发布:淘宝电脑端描述图尺寸 编辑:程序博客网 时间:2024/06/05 21:17

<configSections>

  <!-- Initialize the AspNetForums Configuration Handler  -->
  <sectionGroup name="forums">
   <section name="forums" type="AspNetForums.Configuration.ForumsConfigurationHandler, AspNetForums.Components" />
  </sectionGroup>

 </configSections>

里定义了一个配置节处理程序声明(Section),按照规定它们必须出现在配置文件顶部 <configSections> 和 </configSections> 标记之间,在这里,它们只用到了name和type属性,其中,name属性定义了指定配置节的名称,而type属性则规定了指定从配置文件中读取节的配置节处理程序类的名称,有两个部分,前面为处理程序的类名,后面为Assembly名(Assembly必须位于bin目录中)以及版本号,公匙等信息。 
   他们具体表示什么意思呢?比如第一个section,意思就是告诉Asp.Net系统,当在程序中使用System.Configuration.ConfigurationSettings.GetConfig("forums/forums")这个静态方法来读取ApplicationConfiguration配置节的时候,会调用AspNetForums.Configuration.ForumsConfigurationHandler这个类来对这个配置节进行处理

system.web 节 呵,所有asp.net程序都有的这里特别注意的是
 <httpModules>
   <add name="AspNetForums" type="AspNetForums.ForumsHttpModule, AspNetForums.Components" />
  </httpModules>

  <httpHandlers>
   <add verb="GET" path="avatar.aspx" type="AspNetForums.Components.HttpHandler.AvatarHttpHandler, AspNetForums.Components" />
   <add verb="GET" path="vcard.aspx" type="AspNetForums.Components.HttpHandler.VCardHttpHandler, AspNetForums.Components" />
  </httpHandlers>

 当向IIS发出请求时,IIS会把请求转交给asp.net HTTP管道 ,HTTP管道入口是HttpRuntime类的实体 。hTTPRuntime 类实体从内部程序池中选择一个HttpApplication 对像,并且在接收到新的请求时使它工作。Http应用管道程序的主要工作是寻找这样的类实体(处理句柄)使它处理相应的请求。
比如*.aspx通过PageHandlerFactory句柄处理. 实现IIS里的文件后缀名映射功能.

IIS asp.net 处理请求示意图

          客户端请求
                  |
                  |
                 IIS   //交给IIS
                  |
                  |
           W3WP.exe   //asp.net处理进
                  |
                  |                //HttpRuntime  可以理解当前处理实例 
          HttpAppliction  ---------------HttpModule
                  |
                  |
          HttpHandler

IHttpHandler实现了对IIS输入输出的管理和扩展 ,HttpModule接管所有输入输出,查找相符的请求进行处理。

   
 <httpHandlers>节 verd属性,请求类型 可以是get,put,post的任意组合用逗号分隔
path 要处理的程序路径 ,可以是单个URL或通配符
type 实现IhttpHandler的类型

  当向该论坛发出请求时执行顺序如下:

      System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)


 System.Web.SyncEventExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()

AspNetForums.ForumsHttpModule.Application_BeginRequest(Object source, EventArgs e)    //从这里开始交给自写义的HttpModule

 <httpModules>
   <add name="AspNetForums" type="AspNetForums.ForumsHttpModule, AspNetForums.Components" />
  </httpModules>

   http请求会被AspNetForums.ForumsHttpModule接管 ,查看此类型
  其实aspnetforum的初始化工作也是从这里开始的 


  该类型定义
 public class ForumsHttpModule : IHttpModule
 实现 IHttpModule接口  
该接口要实现Init()和Dispose()二个方法

 Init方法中声时的事件:
application.BeginRequest += new EventHandler(this.Application_BeginRequest);
  .............................
当第一个请求时:

事件处理:

 private void Application_BeginRequest(Object source, EventArgs e) {
   
   try {
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;

    if( application == null
     || context  == null )
     return;

    // Url Rewriting
    //
    RewriteUrl(context);

    // Create the forum context
    //封装自定义的Context
    context.Items.Add("ForumContext", new ForumContext());

    // Capture any pingback information
    //
    CaptureForumPingback();

    // Are the forums disabled?
    //检查是否开启论坛和当前请求是否在本机运行
    if ((Globals.GetSiteSettings().ForumsDisabled) && (HttpContext.Current.Request.Url.Host != "localhost"))  {

  //调用GetConfig()方法
  该方法返回实例化一个ForumConfiguration返回
   当调用 ConfigurationSettings.GetConfig("forums/forums");
 会在配置节查找 <sectionGroup name="forums">
   <section name="forums" type="AspNetForums.Configuration.ForumsConfigurationHandler, AspNetForums.Components" />
  </sectionGroup>
 找到处理类型 
AspNetForums.Configuration.ForumsConfigurationHandler

通过该类型处理
<forums><forums
   defaultProvider="SqlForumsProvider"
   defaultLanguage="zh-CN"
   forumFilesPath="/"
   disableEmail="true"
   disableIndexing="true"
   disableThreading="true"
   threadIntervalStats="15"
   threadIntervalEmail="3"
   passwordEncodingFormat="unicode"
   allowAutoUserRegistration="false"
   adminWindowsGroup="Administrators"
   assignLocalAdminsAdminRole="false"
   smtpServerConnectionLimit="-1"
   enableLatestVersionCheck="false"
   uploadFilesPath="/Upload/"
  >

   <providers>

    <clear/>
 
    <add
     name = "SqlForumsProvider"
     type = "AspNetForums.Data.SqlDataProvider, AspNetForums.SqlDataProvider"
     connectionString = ""
     databaseOwner = "dbo"
    />

   </providers>
  </forums>
 </forums>
 节点,来设置
    ForumConfiguration 类型实例  

 相当于
  ForumConfiguration fc =new ForumConfiguration();
  fc.LoadValuesFromConfigurationXml(node); 
node 是设置节节点 
  
  这里初始化工作已经基本完成 :)

    

<providers>

    <clear/>

    <add
     name = "SqlAccountProvider"
     type = "Account.Framework.Data.SqlDataProvider, Account.Framework"
     connectionString = ""
     databaseOwner = "dbo"
    />

   </providers>


 name设置缓存中数据处理类型键  ,Account.Framework.Data.SqlDataProvider数据类型
呵,这个非常灵话 
 比如需要实现一个Access数据库版本的 ,那么程序不用做任何改动 重写一个Account.Framework.Data.OledbDataProvider 类型,继承ForumsDataProvider方法就可以了
 

原创粉丝点击