架构深渊

慢慢走进程序的深渊……关注领域驱动设计、测试驱动开发、设计模式、企业应用架构模式……积累技术细节,以设计架构为宗。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

httpModule 伪静态

Posted on 2008-10-09 20:04  chen eric  阅读(413)  评论(0编辑  收藏  举报
C# code
public class HttpModule : System.Web.IHttpModule { /// <summary> /// 实现接口的Init方法 /// </summary> /// <param name="context"></param> public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(ReUrl_BeginRequest); } public void Application_OnError(Object sender, EventArgs e) { } /// <summary> /// 实现接口的Dispose方法 /// </summary> public void Dispose() { } /// <summary> /// 重写Url /// </summary> /// <param name="sender">事件的源</param> /// <param name="e">包含事件数据的 EventArgs</param> private void ReUrl_BeginRequest(object sender, EventArgs e) { //---------------------// //string templateid = "1"; //---------------------// HttpContext context = ((HttpApplication)sender).Context; Ccdodo.Entity.ConfigInfo Config = Ccdodo.XmlLib.XmlData.Config; //string SitePath = config.SitePath; string requestPath = context.Request.Path.ToLower(); //string path = requestPath.Substring(Config.SitePath.Length + 5); // if (requestPath.StartsWith(Config.SitePath + "bbs") || requestPath.StartsWith(Config.SitePath + "/bbs")) { context.RewritePath(Config.SitePath + requestPath); //System.Web.HttpContext.Current.Response.Write(requestPath + ":" + Config.SitePath + requestPath + ":" + requestPath.StartsWith(Config.SitePath + "bbs/")); //System.Web.HttpContext.Current.Response.End(); } else { foreach (SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls) { //System.Web.HttpContext.Current.Response.Write(Regex.IsMatch(path, url.Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase).ToString() + "<br/>"); //System.Web.HttpContext.Current.Response.Write(Config.SitePath + url.Page + "<br/>" + newUrl + "<br/><br/>" + requestPath + "<br/>" + url.Pattern); if (Regex.IsMatch(requestPath, url.Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase)) { //实际地址 string newUrl = Regex.Replace(context.Request.Path, url.Pattern, url.QueryString, RegexOptions.Compiled | RegexOptions.IgnoreCase); //System.Web.HttpContext.Current.Response.Write(Regex.IsMatch(requestPath, url.Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase).ToString() + "<br/>"); //System.Web.HttpContext.Current.Response.Write(Config.SitePath + url.Page + "<br/>" + newUrl + "<br/><br/>" + requestPath + "<br/>" + url.Pattern); //System.Web.HttpContext.Current.Response.End(); context.RewritePath(Config.SitePath + url.Page, string.Empty, newUrl); return; } // } } context.RewritePath(Config.SitePath + requestPath); //--// } }