.text urlRewrite介绍
点击次数:16 次 发布日期:2008-11-27 00:00:27 作者:源代码网
|
源代码网推荐 第一次拿到dottext时,开始让我比较觉得比较奇怪的是 源代码网推荐 源代码网推荐 一、以floerggyy注册后通过URL:http://x.x.x.x/floerggyy即可进入自己的blog里 源代码网推荐 (其实忘了以前常做下载页面download.aspx也不过是处理了HttpHandler的虚页面而已,可能是见在.Text兴奋的连这些基本常识都忘了^_^) 源代码网推荐 源代码网推荐 二、居然可以拿用户名做用户的唯一标识但在表里面没有找到做为用户名UserName唯一约束的东东(到现在还不清楚在数据库哪个地方设置的,有知道的请指点下) 源代码网推荐 后来通过重得注册同一用户名查看抛出的异常信息,确认确实在有UserName做为唯一约束的东东。 源代码网推荐 唉,看来我对数据库一无所知。 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 ...后来决定专写一篇关于URL重写的文章,后来看到dottext的原作者也简单介绍了下urlRewrite,于是这个想法就放弃了。 源代码网推荐 后来又有一些朋友问dottext关于URL的问题,看来还是写吧 源代码网推荐 源代码网推荐 2、配置文件WebConfig.config简单浏览 源代码网推荐 源代码网推荐 自定义配置节内容: 源代码网推荐 <configSections> 源代码网推荐 <section name="BlogConfigurationSettings" type="Dottext.Framework.Util.XmlSerializerSectionHandler, Dottext.Framework" /> 源代码网推荐 <section name="HandlerConfiguration" type="Dottext.Framework.Util.XmlSerializerSectionHandler, Dottext.Framework" /> 源代码网推荐 <section name="SearchConfiguration" type="Dottext.Framework.Util.XmlSerializerSectionHandler, Dottext.Framework" /> 源代码网推荐 <section name="microsoft.web.services" type="Microsoft.Web.Services.Configuration.WebServicesConfiguration, Microsoft.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 源代码网推荐 <section name="codeHighlighter" type="ActiproSoftware.CodeHighlighter.CodeHighlighterConfigurationSectionHandler, ActiproSoftware.CodeHighlighter" /> 源代码网推荐 </configSections> 源代码网推荐 源代码网推荐 HttpHandler的配置内容: 源代码网推荐 <httpHandlers> 源代码网推荐 <add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 源代码网推荐 validate="false" /> 源代码网推荐 <add verb="*" path="Error.aspx" type="System.Web.UI.PageHandlerFactory" /> 源代码网推荐 <add verb="*" path="*" type="Dottext.Common.UrlManager.UrlReWriteHandlerFactory,Dottext.Common" /> 源代码网推荐 </httpHandlers> 源代码网推荐 源代码网推荐 HttpModule的配置内容: 源代码网推荐 <httpModules> 源代码网推荐 <add name="UrlReWriteModule" type="Dottext.Common.UrlManager.UrlReWriteModule, Dottext.Common" /> 源代码网推荐 <add name="EventHttpModule" type="Dottext.Framework.ScheduledEvents.EventHttpModule, Dottext.Framework" /> 源代码网推荐 </httpModules> 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 见到一个陌生的项目首先打开它的配置文件看看,这是我的习惯:) 源代码网推荐 先看看一些重点的配置内容: 源代码网推荐 源代码网推荐 看完Web.config中的上述内容熟悉asp.net运行机制的朋友就明白,DotText代码的运行顺序。在这里我再简单重复下 源代码网推荐 aspnet的内部运行机制(若有不熟悉的朋友请参阅<<ASP.NET FameWork深度历险>>这本书,它对做asp.net开发的朋友很有帮助): 源代码网推荐 remote client Request---->IIS---->aspnet_isapi.dll-->aspnet_wp.exe-->HttpRuntime---> 源代码网推荐 HttpModule--->HttpHandler Factory--->HttpHandler--->HttpHandler.ProcessRequest()-->Response client Request 源代码网推荐 好了,题归正转,client Request首先是被HttpModule截获。当我们请求.text的URL:http://www.cnblogs.com/floerggyy/时,首先是 源代码网推荐 Dottext.Common.UrlManager命名空间下类UrlReWriteModule的相关方法被调用。 源代码网推荐 (为什么会被类UrlReWriteModule截获远程请求呢?上面HttpModule配置节的内容不是标明了吗???^_^ 源代码网推荐 明知故问,那么为Dottext.Framework.ScheduledEvents命名空间下的类EventHttpModule会不会截获远程请求?什么时候截获呢? 源代码网推荐 当然是按先来后顺序了,中国的优良传统都忘了!!! 源代码网推荐 (其实这样说也是不太准确的,这两个HttpModule确是按顺序执行的但在HttpModule里的一些事件中它们是交叉运行的,好了类EventHttpModule 源代码网推荐 不在我们的计论范围内在下面的代码就不分析了,有对这块不明白的最好去看下上面推荐的那本书^_^) 源代码网推荐 源代码网推荐 3 、URL重写,部分代码分析(这块涉及到众多自定义配置节、HttpModule、HttpHandler的综合应用所以要理顺还是有点麻烦的,要有一小点分析别人代码的耐心。个人认为) 源代码网推荐 源代码网推荐 类UrlReWriteModule的方法 源代码网推荐 源代码网推荐 源代码网推荐 private void context_BeginRequest(object sender, EventArgs e){ 源代码网推荐 //它是主要作用是根据请求匹配正则表达式来设置是否重写客户所请求的URL(它默认是重写URL),注意这句代码UrlHelper.SetEnableUrlReWriting(context,false); 源代码网推荐 if(ConfigProvider.Instance().IsAggregateSite){ 源代码网推荐 HttpContext context = ((HttpApplication)sender).Context; 源代码网推荐 源代码网推荐 string path = context.Request.Path.ToLower(); 源代码网推荐 int iExtraStuff = path.IndexOf(".aspx"); 源代码网推荐 if(iExtraStuff > -1 || path.IndexOf(".") == -1) { 源代码网推荐 if(iExtraStuff > -1) 源代码网推荐 { 源代码网推荐 path = path.Remove(iExtraStuff+5,path.Length - (iExtraStuff+5)); 源代码网推荐 } 源代码网推荐 源代码网推荐 path = regexApplication.Replace(path,string.Empty,1,0); 源代码网推荐 源代码网推荐 if(path == "" || path == "/" || regexPath.IsMatch(path)) 源代码网推荐 { 源代码网推荐 UrlHelper.SetEnableUrlReWriting(context,false); 源代码网推荐 } 源代码网推荐 源代码网推荐 }else if(context.Request.Path.ToLower().IndexOf("services") > 0 && context.Request.Path.ToLower().IndexOf(".asmx") > 0 ) 源代码网推荐 { 源代码网推荐 if(AlllowService(context)) 源代码网推荐 { 源代码网推荐 if(context.Request.RequestType!="POST") 源代码网推荐 { 源代码网推荐 string regexstr=@"/w+/services/"; 源代码网推荐 string url=Regex.Replace(context.Request.RawUrl,regexstr,"/services/",RegexOptions.IgnoreCase); 源代码网推荐 context.RewritePath(url); 源代码网推荐 } 源代码网推荐 //string fileName =context.Request; //System.IO.Path.GetFileName(context.Request.Path); 源代码网推荐 //context.RewritePath("~/Services/" + fileName); 源代码网推荐 }else{ 源代码网推荐 context.Response.Clear(); 源代码网推荐 context.Response.End(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 HttpModule处理完后(这句话并不正确,在这里是这样的)进入HttpHandler Factory,根据HttpHandler的配置内容我们可以马上找到这个类 源代码网推荐 UrlReWriteHandlerFactory它是处理重写URL请求核心,在这里我详细分析下。 源代码网推荐 它实现了IHttpHandlerFactory 源代码网推荐 (看注释就知道这个类是很重要的了) 源代码网推荐 源代码网推荐 源代码网推荐 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); 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 private IHttpHandler ProccessHandlerTypePage(HttpHandler item, HttpContext context, string requestType, string url) 源代码网推荐 { 源代码网推荐 string pagepath = item.FullPageLocation; 源代码网推荐 if(pagepath == null) 源代码网推荐 { 源代码网推荐 pagepath = HandlerConfiguration.Instance().FullPageLocation; 源代码网推荐 } 源代码网推荐 HandlerConfiguration.SetControls(context,item.BlogControls); 源代码网推荐 IHttpHandler myhandler=PageParser.GetCompiledPageInstance(url,pagepath,context); 源代码网推荐 return myhandler; 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 public virtual void ReleaseHandler(IHttpHandler handler) 源代码网推荐 { 源代码网推荐 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 要注意它是如何把自定义配置节中的内容拈合成httphandler的实例 源代码网推荐 把这些理顺后对于理解.text的url重写就不难了.... 源代码网推荐 源代码网推荐 对上面若有理解不正解的欢迎高手指正 源代码网推荐 源代码网推荐 源代码网供稿. |
