首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ASP.NET >> 正文

asp.net2.0 URL重写以及urlMappings问题(1)

  在asp.net2.0中的urlMappings倒是非常好用,可惜暂不支持正则表达式,不过,好在如果用IHttpModule的话
  
  不管什么样的请求都会先经过IHttpModule这样就为URL重写提供了一个好机会:
  
  下面是我写的一个IHttpModule:
  
  using System;
  using System.Web;
  
  public class ReWriteModule:IHttpModule
  {
   public ReWriteModule()
   {
   }
   public override string ToString()
   {
   return this.GetType().ToString();
   }
  
  
  void IHttpModule.Dispose()
  {
  
  }
   private static System.Xml.XmlDocument ruleDoc = null;
   private static System.Xml.XmlDocument GetRuleConfig(System.Web.HttpContext app)
   {
   if (ruleDoc == null)
   {
   ruleDoc = new System.Xml.XmlDocument();
   ruleDoc.Load(app.Server.MapPath("~/rule.xml"));
   }
   return ruleDoc;
   }
   public static string GetUrl(System.Web.HttpContext cxt,string path)
   {
  
   System.Xml.XmlDocument doc = GetRuleConfig(cxt);
   System.Xml.XmlNodeList lst= doc.GetElementsByTagName("RewriterRule");
   string pat="";
   foreach (System.Xml.XmlNode nd in lst)
   {
   System.Xml.XmlNodeList sub = nd.ChildNodes[0].ChildNodes;
   foreach(System.Xml.XmlNode chk in sub)
   {
   pat = "^" + chk.InnerText+"$";
   System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pat, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
   if(reg.IsMatch(path))
   {
   return reg.Replace(path, nd.ChildNodes[1].InnerText);
   }
   }
   }
   return null;
  
   }
  void IHttpModule.Init(HttpApplication context)
  {
   context.BeginRequest += delegate(object sender, EventArgs e)
   {
  
  
   System.Web.HttpContext cxt = context.Context;
  
   if (cxt.Request.ContentType != "image/pjpeg")
   {
   string type = cxt.Request.ContentType.ToLower();
   string path = cxt.Request.Path;
   string apppath = cxt.Request.ApplicationPath;
   path = path.Remove(0, apppath.Length);
   path = "~" + path;
  
   string newUrl = GetUrl(cxt, path.TrimEnd().TrimStart());
   if (newUrl != null)
   {
   cxt.Response.Filter = new ResponseFilter(cxt.Response.Filter,cxt.Request.Path);
   cxt.Response.Write("请求的路径:" + path);
   cxt.Response.Write("
");
   cxt.Response.Write("转向的目的URL:" + newUrl);
   cxt.Response.Write("
");
   cxt.RewritePath(newUrl);
  
  
  
   }//如果要求处理所有的请求时用到
   //else
   //{
   // cxt.Response.Write(cxt.Request.Path + "
");
   // cxt.Response.Write("你请求的资源不存在或无权访问!");
   // cxt.Response.Flush();
   // cxt.Response.End();
   //}
   }
  
   };
   }
  
  
  }
【责编:Kittoy】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·开源软件测试工具学习专题
 ·JSP Web开发 入门基础到高手进阶教程…
 ·JavaFX—是Java桌面的新希望么?
 ·安全至上 .NET开发安全策略…
 ·测试用例设计之道-测试用例学习专题
 ·面向Java开发人员的Scala指南
 ·Java设计模式之实例详解
 ·Oracle数据库11g 面向DBA和开发人员的重要新特性…
 ·桌面应用软件编程 J2SE技术详解…
 ·我“炫”我精彩-------WPF开发教程
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航