httpmodule处理完后(这句话并不正确,在这里是这样的)进入httphandler factory,根据httphandler的配置内容我们可以马上找到这个类 urlrewritehandlerfactory它是处理重写url请求核心,在这里我详细分析下。 它实现了ihttphandlerfactory (看注释就知道这个类是很重要的了) using system; using system.web; using system.web.ui; using system.text.regularexpressions;
using dottext.framework; using dottext.framework.components; using dottext.framework.configuration;
namespace dottext.common.urlmanager { /**//// <summary> /// class responisble for figuring out which .text page to load. by default will load an array of dottext.urlmanager.httphanlder /// from the blog.config file. this contains a list of regex patterns to match the current request to. it also allows caching of the /// regex’s and types /// </summary> public class urlrewritehandlerfactory: ihttphandlerfactory { public urlrewritehandlerfactory(){} //nothing to do in the cnstr //自定义虚方法从自定义配置节内容反序列化时构造httphandler protected virtual httphandler[] gethttphandlers(httpcontext context) { return handlerconfiguration.instance().httphandlers; }
/**//// <summary> /// implementation of ihttphandlerfactory. by default, it will load an array of httphanlder (dottext.urlmanager.httphandler) from /// the blog.config. this can be changed, by overrideing the gethttphandlers(httpcontext context) method. /// </summary> /// <param name="context">current httpcontext</param> /// <param name="requesttype">request type (passed along to other ihttphandlerfactory’s)</param> /// <param name="url">the current requested url. (passed along to other ihttphandlerfactory’s)</param> /// <param name="path">the physical path of the current request. is not gaurenteed to exist (passed along to other ihttphandlerfactory’s)</param> /// <returns> /// returns an instance of ihttphandler either by loading an instance of ihttphandler or by returning an other /// ihttphandlerfactory.gethanlder(httpcontext context, string requesttype, string url, string path) method /// </returns> //实现接口ihttphandlerfactory定义的方法 public virtual ihttphandler gethandler(httpcontext context, string requesttype, string url, string path) { //get the handlers to process. by defualt, we grab them from the blog.config httphandler[] items = gethttphandlers(context); //dottext.framework.logger.logmanager.log("path",dottext.framework.util.globals.removeappfrompath(context.request.path,context.request.applicationpath)); //do we have any? if(items != null) { int count = items.length;
for(int i = 0; i<count; i++) { //we should use our own cached regex. this should limit the number of regex’s created //and allows us to take advantage of regexoptons.compiled //逐个匹配所配置节中定义的请求类型 if(items[i].ismatch(dottext.framework.util.globals.removeappfrompath(context.request.path,context.request.applicationpath))) { //注意这里是关键,注意返回的httphandler实例 //throw new exception(); switch(items[i].handlertype) { case handlertype.page://默认是page
return proccesshandlertypepage(items[i],context,requesttype,url); case handlertype.direct: handlerconfiguration.setcontrols(context,items[i].blogcontrols); return (ihttphandler)items[i].instance(); case handlertype.factory: //pass a long the request to a custom ihttphandlerfactory return ((ihttphandlerfactory)items[i].instance()).gethandler(context,requesttype,url,path); default: throw new exception("invalid handlertype: unknown"); } } } } //if we do not find the page, just let asp.net take over return pagehandlerfactory.gethandler(context,requesttype,url, path); }