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

整理一下dottext中的UrlRewrite流程

2012年06月16日 ⁄ 综合 ⁄ 共 3416字 ⁄ 字号 评论关闭

整理归纳,以作备忘(欢迎指正)
UrlRewrite的原理可参考http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

从web.config出发


<httpModules>
           
<add name="UrlReWriteModule" type="Dottext.Common.UrlManager.UrlReWriteModule, Dottext.Common" />
           
<add name="EventHttpModule" type="Dottext.Framework.ScheduledEvents.EventHttpModule, Dottext.Framework" />
           
<!--<add name="MsftBlogsHttpModule" type= "AspNetWeb.MsftBlogsHttpModule, MsftBlogsHttpModule" />-->
 
</httpModules>
UrlReWriteModule:Init()中把context_BeginRequest()绑定到HttpApplication的BeginRequest事件,注意此函数先于Application_BeginRequest()函数运行
 private void context_BeginRequest(object sender, EventArgs e)
-->其中判断条件if(ConfigProvider.Instance().IsAggregateSite) 
-->ConfigProvider:Instance()
-->因为有静态构造函数,其中初始化一系列对象,使用XML序列反序列到对象,最后一直到
    MultipleBlogConfig:
     public override bool IsAggregateSite
      {
        get {return true;}
      }
     由此可知一开始的初始条件为true,返回
-->其中UrlHelper.SetEnableUrlReWriting(context,false);
     我不知道这有什么作用,但搜索全文,未见有引用UrlHelper:GetEnableUrlReWriting(),返回
-->接着,对提供web service进行url修改,context.RewritePath(url);

---------------------------------------------
下面来看EventHttpModule类,实现了定时器功能
-->ScheduledEventWorkCallback
-->EventManager.Execute();
     其中又是一系列的类初始化,转向到SqlDataProvider,有两个函数GetLastExecuteScheduledEventDateTime()
     和SetLastExecuteScheduledEventDateTime()获取更新时间,
-->ManagedThreadPool.QueueUserWorkItem(new WaitCallback(e.Execute));  
    涉及复杂的线程操作 

----------------------------------------------
接着httpApplication在执行了一系列顺序操作后,转到执行处理程序
<httpHandlers>         
            <!-- Can not see to load asmx like .aspx, since we will grap all requests later, make sure these are processed by their default factory -->
           
<add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
           
<!--Since we are grabbing all requests after this, make sure Error.aspx does not rely on .Text -->
           
<add verb="*" path="Error.aspx" type="System.Web.UI.PageHandlerFactory" />
           
<!--This will process any ext mapped to aspnet_isapi.dll -->
           
<add verb="*" path="*" type="Dottext.Common.UrlManager.UrlReWriteHandlerFactory,Dottext.Common" />
</httpHandlers>

可见,当遇到前两者时,是由系统默认对象处理的,除此之外的其它,则转交给UrlReWriteHandlerFactory对象
-->public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string path)
-->GetHttpHandlers
     取得一系列的httpHandler对象,对应于web.config中一大堆的<HttpHandler ....>
这里不得不提一下XmlSerializerSectionHandler对象,在dottext中,几乎都是有个instance函数,或静态构造函数,调用getconfig()来取web.config的xml元素,通过XmlSerializerSectionHandler:Create函数来取得对应的type属性,实例化所需的对象
-->items[i].IsMatch(...)
-->HttpHandler
    public bool IsMatch(string url)
      {
       return UrlRegex.IsMatch(url);
      }
    其中UrlRegex一开始为null,注意这里不能用快速监视,因为监视了第一次后,判断为null会执行代码,返回
-->i=33时,匹配"/default.aspx"
--> ProccessHandlerTypePage()
-->注意defaultPageLocation和pageLocation的关系
-->最关键是这句HandlerConfiguration.SetControls(context,item.BlogControls);
     实质就是把<HttpHandler>中指定的control添加到context.items里,达到动态加载.ascx(UC)的效果,返回
-->最后由PageParser.GetCompiledPageInstance返回IHttpHandler

------------------------------------------------
加载default.aspx的时候,除了MasterPage 机制外,别忽略了DottextMasterPage
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="Dottext.Web.UI.Pages.DottextMasterPage"%>
DottextMasterPage里含有相应的加载代码

------------------------------------------------
由此可见,web.config中多个<HttpHandlere>元素的排列、正则构造以及其它属性安排,都是十分严谨的

抱歉!评论已关闭.