在URL重写中,最重要的莫过于IHttpModule接口了,先看下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace YouXi.Components { public class HttpModule : IHttpModule { #region IHttpModule 成员 void IHttpModule.Dispose() { } void IHttpModule.Init(HttpApplication context) { //在页面读取开始时,这里每个页面都会先运行 context.BeginRequest += new EventHandler(context_BeginRequest); } void context_BeginRequest(object sender, EventArgs e) { //这儿处理url重写 UrlRewriter.Rewriter(); } #endregion } } UrlRewriter类,重写URL,并设置重写路径, using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace YouXi.Components { public class UrlRewriter { ////// url路径重写 /// /// public static void Rewriter() { //这儿处理url重写 //string s_NewUrl = null; //bool IsMatch = false; HttpRequest req = HttpContext.Current.Request; string rawUrl = req.Url.ToString();//.Remove(0, 7).ToLower(); if (!rawUrl.EndsWith(".shtm")) return; if(CommonHelper.UrlReWriterHelper.Rewrite(ref rawUrl)) HttpContext.Current.RewritePath(rawUrl); } } } 重写初始化类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Xml; using System.Text.RegularExpressions; namespace CommonHelper { public class UrlReWriterHelper { private static string configFile = "/Configs/UrlReWriter.Config"; public ListRules; private static UrlReWriterHelper Instance { get; set; } private bool _Enabled; private bool _Reverse; /// /// 反向匹配 /// public bool Reverse { get { return _Reverse; } } ////// 正向匹配 /// public bool Enabled { get { return _Enabled; } } ////// 初始化重写规则 /// static UrlReWriterHelper() { Instance = new UrlReWriterHelper(); Instance.Rules = new List(); string file = HttpContext.Current.Server.MapPath(configFile); XmlDocument xd = new XmlDocument(); xd.Load(file); XmlElement xe = xd.DocumentElement; bool enabled = true; if (xe.Attributes["enabled"] != null) { enabled = Boolean.TryParse(xe.Attributes["enabled"].Value, out enabled); } Instance._Enabled = enabled; bool reverse = true; if (xe.Attributes["reverse"] != null) { reverse = Boolean.TryParse(xe.Attributes["reverse"].Value, out reverse); } Instance._Reverse = reverse; foreach (XmlNode xn in xe.ChildNodes) { if (xn.NodeType == XmlNodeType.Element) { UrlReWriterRule re = new UrlReWriterRule(xn); Instance.Rules.Add(re); } } } /// /// 按配置的规则进行路径重写并将最后值改写给参数,返回值表示是否进行重写 /// /// 可以根据此值进行重写操作 ///public static bool Rewrite(ref string rawUrl) { if (string.IsNullOrEmpty(rawUrl.Trim())) return false; UrlReWriterHelper config = UrlReWriterHelper.Instance; if (!config.Enabled) return false; List Rules = config.Rules; if (Rules == null || Rules.Count == 0) return false; foreach (UrlReWriterRule rule in Rules) { Regex r = new Regex(rule.Pattern, RegexOptions.IgnoreCase); if (r.IsMatch(rawUrl)) { string s_url = r.Replace(rawUrl, rule.Url); rawUrl = s_url.Replace("/","/"); int start = rawUrl.IndexOf("/", 7); if (start == 0) return true; rawUrl = rawUrl.Substring(start); return true; } } return false; } } } UrlReWriterRule规则类, using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace CommonHelper { /// /// URL重写配置项 /// public class UrlReWriterRule { #region private string _Pattern; private string _Url; private string _RePattern; private string _ReUrl; #endregion #region ////// 反向重写后的路径 /// public string ReUrl { get { return _ReUrl; } } ////// 反向原始路径 /// public string Repattern { get { return _RePattern; } } ////// 正向重写后的路径 /// public string Url { get { return _Url; } } ////// 正向原始路径 /// public string Pattern { get { return _Pattern; } } #endregion public UrlReWriterRule() { } ////// 构造函数 /// /// XML节点 public UrlReWriterRule(XmlNode xn) { try { if (xn.SelectSingleNode("pattern") != null) _Pattern = xn.SelectSingleNode("pattern").InnerText; else if(xn.Attributes["pattern"]!=null) _Pattern = xn.Attributes["pattern"].Value; if (xn.SelectSingleNode("url") != null) _Url = xn.SelectSingleNode("url").InnerText; else if (xn.Attributes["url"] != null) _Url = xn.Attributes["url"].Value; if (xn.SelectSingleNode("repattern") != null) _RePattern = xn.SelectSingleNode("repattern").InnerText; else if (xn.Attributes["repattern"] != null) _RePattern = xn.Attributes["repattern"].Value; if (xn.SelectSingleNode("reurl") != null) _ReUrl = xn.SelectSingleNode("reurl").InnerText; else if (xn.Attributes["reurl"] != null) _ReUrl = xn.Attributes["reurl"].Value; } catch (Exception e) { throw (e); } } } } UrlReWriter.Config 重写规则config文件