现在的位置: 首页 > 综合 > 正文

Discuz!NT代码阅读笔记(1)–从HttpModule开始

2018年01月23日 ⁄ 综合 ⁄ 共 2799字 ⁄ 字号 评论关闭

 

一、从HttpModule开始:首页的显示

         解压下载来的代码包,打开Discuz_vs08解决方案文件,可以看见这个解决方案有24个项目。最后生成网站输出的项目是Discuz.Web。展开这个项目,发现熟悉的index.aspx文件、两个config文件和几个文件夹。

         双击index.aspx文件,发现里边只有这么短短的一行代码:

<%@ Page %>

运行后,却发现首页有很多丰富的内容。其实这行代码什么都没做,删掉它,依然你能正常显示首页。为什么呢?看下边的分析。

 

打开Web.Config文件,可以看见这样一段代码:

    <httpModules>

      <add type="Discuz.Forum.HttpModule, Discuz.Forum" name="HttpModule" />

</httpModules>

和:

         <modules>

      <!--  注意:此节设置由Discuz!NT接管http请求。不会干涉对非Discuz!NT论坛路径下的请求。-->

              <add type="Discuz.Forum.HttpModule, Discuz.Forum" name="HttpModule" />

     </modules>

这里注册了httpModules这个东西,上边的代码在IIS6中有效,下边的在IIS7中有效。

自己注册的httpModules由程序集Discuz.Forum中的类 Discuz.Forum.HttpModul接管了Http请求。即: 程序一开始运行,就会跳转到Discuz.Forum.HttpModul这个类中,在这个类中完成首页的显示和跳转。

关于HttpModule的介绍,可以看我在博客中转贴自张子阳的《Http 请求处理流程》和《Http Module 介绍 这两篇文章介绍了程序为什么会跳转到Discuz.Forum.HttpModul类。

在项目Discuz.Forum中找到文件HttpModule.cs。可以看到HttpModule.cs中有一个叫HttpModule的类 。这个类继承了接口System.Web.IHttpModule并实现了IHttpModuleInitDispose方法。

Init方法中注册了函数ReUrl_BeginRequest事件用来处理Http请求:

context.BeginRequest += new EventHandler(ReUrl_BeginRequest);

注册这个事件之后,每次有Http请求,就会触发这个事件。

找到ReUrl_BeginRequest方法。

在这个函数中,首先从配置文件config/general.config中读取论坛程序配置信息,如下:

GeneralConfigInfo config = GeneralConfigs.GetConfig();

之后通过对config中内容的判断,来完成论坛信息的初始化。如是否启用Url重写,是否使用伪aspx等等。

收集了这些信息走后,在下边的代码将Http请求进行重定向:

                 if (requestPath.EndsWith("/index.aspx"))//如果是否首页的请求

                    {

                        if (config.Indexpage == 0) //是否显示聚合首页

                        {

                            if (config.BrowseCreateTemplate == 1)//模板不存在时是否自动生成

                            {

                                CreateTemplate(forumPath, Templates.GetTemplateItem(int.Parse(strTemplateid)).Directory, "forumindex.aspx",int.Parse(strTemplateid));

                            }

                            context.RewritePath(forumPath + "aspx/" + strTemplateid + "/forumindex.aspx");//显示论坛首页

                        }

                        else

                        {

                            if (config.BrowseCreateTemplate == 1)

                            {

                                CreateTemplate(forumPath, Templates.GetTemplateItem(int.Parse(strTemplateid)).Directory, "website.aspx",int.Parse(strTemplateid));

                            }

                            context.RewritePath(forumPath + "aspx/" + strTemplateid + "/website.aspx");//显示聚合首页

                        }

                        return;

                    }

config.Indexpage标识了论坛首页显示信息: 显示聚合首页或者是论坛首页。如果显示论坛首页,就通过HttpContextRewritePath跳转到aspxstrTemplateid目录中的forumindex.aspx

显示论坛首页,否则,就跳转到website.aspx显示聚合首页。

HttpContext中的RewritePath指定了内部重写路径,RewritePath 方法会将对资源的请求重新定向到其他资源,同时不会更改 URL,因此我们看到的首页还是Index.aspx

 

论坛其他页面的显示也可以通过跟踪ReUrl_BeginRequest函数来获取。

 

 

 

http://www.cnblogs.com/zyqgold/archive/2010/04/25/1720791.html

 

抱歉!评论已关闭.