logo 广告
Loading...
 导航 所在位置:论坛首页 -> ┈┋电脑网络┋┈ -> 网页制作技术 -> asp.net 2.0中的URL重写以及urlMappings问题
回复
标题:asp.net 2.0中的URL重写以及urlMappings问题收藏 编辑 删除 楼主 | 上一篇 下一篇
年少轻狂
头像
等级:社区游侠
权限:普通用户
积分:37
金钱:3647
声望:64
经验:64
发帖数:861
注册:2007年10月26日
资料 短消息2008-4-27 10:57:27

在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("<BR>"); 
   cxt.Response.Write("转向的目的URL:" + newUrl); 
   cxt.Response.Write("<BR>"); 
   cxt.RewritePath(newUrl); 
   
   }//如果要求处理所有的请求时用到 
   //else 
   //{ 
   // cxt.Response.Write(cxt.Request.Path + "<BR>"); 
   // cxt.Response.Write("你请求的资源不存在或无权访问!"); 
   // cxt.Response.Flush(); 

   // cxt.Response.End(); 

   //} 

   } 

      }; 

  } 

    }                                    

签名

2008-4-27 10:57:27 顶部
第1页 共页 共0个回复     <<    >>    
 快速回复
  • 支持UBB,HTML标签

  • 高级回复

  • 操作选项:评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
      首页 | 购买指南 | 虚拟主机 | 特色介绍 | 下载中心 | 支付方式
    Copyright 2004-2008 BBSGood.com Powered By: BBSGood.Speed Version 5.0
      咨询电话:0575-85513832、0575-85513825(传真)、7*24小时咨询服务:13606552007 不良信息举报中心 浙ICP备05029817号
      业务QQ:38958768、客服QQ1:415896239、客服QQ2:343896043、MSN:jccsxx@hotmail.com