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:
<Httpmodules>
<Add Type="Discuz. Forum. httpmodule, discuz. Forum"Name="Httpmodule"/>
</Httpmodules>
And:
<Modules>
<! -- 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"/>
</Modules>
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.
FindReurl_beginrequestMethod.
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
}
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");//Show aggregation Homepage
}
Return;
}
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.