Discuz! NT Code Reading Notes (1)-starting from httpmodule

Source: Internet
Author: User

DiscuzIs an open-sourceASP. NETForumProgram,SlaveHttp://www.discuz.com/DownloadableSource code. I saw him from 3.0.Code, The latest version is3.1.I have been getting its Code for a while, so I can check it out every day. Never done beforeASP. NETSomething, just saw the code is a piece of dew. After reading this article for a while, I may have some eyebrows. As a result, according to Lao Zhao, there are gains in writing this article.

It's a cainiao. If something is wrong or the language organization is not good enough, I hope you can correct it.

I. SlaveHttpmoduleStart: display the homepage

Decompress the downloaded code package and open it.Discuz_vs08Solution file, you can see that this solution has24Projects. The project that generates the website output isDiscuz. Web. Expand this project to find familiarIndex. aspxFile, twoConfigFiles and Several folders.

Double-clickIndex. aspxFile, found only such a short line of code:

<%@ Page %>

After running, I found that the homepage has a lot of rich content. In fact, this line of code does not do anything. If you delete it, you can still display the homepage normally. Why? See the analysis below.


OpenWeb. configFile, you can see the following code:


<Add Type="Discuz. Forum. httpmodule, discuz. Forum"Name="Httpmodule"/>




<! -- Note: This section is set by discuz! NT takes over the HTTP request. No interference on non-discuz! Request in the path of the NT forum.-->

<Add Type="Discuz. Forum. httpmodule, discuz. Forum"Name="Httpmodule"/>


Registered hereHttpmodulesThe above code isIIS6Valid.Iis7.

Self-registeredHttpmodulesBy AssemblyDiscuz. ForumClassDiscuz. Forum. httpmodulTake overHTTPRequest. That is: The program jumpsDiscuz. Forum. httpmodulIn this class, the homepage is displayed and redirected.

about httpmodule and HTTP module introduction the Article describes why the program jumps to discuz. forum. httpmodul class.

In the projectDiscuz. ForumFind the fileHttpmodule. CS. We can see thatHttpmodule. CSOne of them isHttpmoduleClass . This class inherits the interfaceSystem. Web.IhttpmoduleAnd implementsIhttpmoduleOfInitAndDisposeMethod.

InInitThe method registers a function.Reurl_beginrequestEvents are used to process HTTP requests:

Context. beginrequest + =New Eventhandler(Reurl_beginrequest );

After registering this eventHTTPThe request will trigger this event.


In this function, first from the configuration fileConfig/General. configReadingConfigure the Forum program as follows:

GeneralconfiginfoConfig =Generalconfigs. Getconfig ();

AfterConfigTo initialize Forum information.If enabled or notURLRewrite, whether to use pseudoAspxAnd so on.

After the information is collectedHTTPRequest redirection:

If(Requestpath. endswith ("/Index. aspx"))//If the homepage request is not sent


If(Config. indexpage = 0 )//Show aggregate homepage?


If(Config. browsecreatetemplate = 1 )//Whether the template is automatically generated if it does not exist


Createtemplate (forumpath,Templates. Gettemplateitem (Int. Parse (strtemplateid). Directory,"Forumindex. aspx",Int. Parse (strtemplateid ));


Context. rewritepath (forumpath +"Aspx /"+ Strtemplateid +"/Forumindex. aspx");//Show Forum Homepage




If(Config. browsecreatetemplate = 1)


Createtemplate (forumpath,Templates. Gettemplateitem (Int. Parse (strtemplateid). Directory,"Website. aspx",Int. Parse (strtemplateid ));


Context. rewritepath (forumpath +"Aspx /"+ Strtemplateid +"/Website. aspx");//Show aggregation Homepage




Config. indexpageThe display information of the Forum homepage is marked: Displays the aggregation homepage or forum homepage. If the Forum homepage is displayed, clickHttpcontextOfRewritepathJumpAspxLowerStrtemplateidDirectoryForumindex. aspx.

The Forum homepage is displayed. Otherwise, the page will jumpWebsite. aspxDisplays the aggregation homepage.

HttpcontextInRewritepathThe internal rewrite path is specified,RewritepathMethod will redirect requests to resources to other resources without changingURLSo the homepage we see isIndex. aspx.

Other pages of the Forum can also be displayed through trackingReurl_beginrequestFunction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.