日期:2014-05-17 浏览次数:20561 次
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text.RegularExpressions; namespace MyModule { /// <summary> ///UrlReWriteHttpModule 的摘要说明 /// </summary> public class UrlReWriteHttpModule : IHttpModule { public UrlReWriteHttpModule() { // //TODO: 在此处添加构造函数逻辑 // } public void Dispose() { //throw new Exception("The method or operation is not implemented."); } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } void context_BeginRequest(object sender, EventArgs e) { if (DictionaryClass.CallToUrlReWriteHttpModule_BOOL_Execute == false) //假如HTTP的请求是重写的.aspx,则返回 { DictionaryClass.CallToUrlReWriteHttpModule_BOOL_Execute = true; return; } HttpContext Context = (sender as HttpApplication).Context; Regex reg1 = new Regex(@"(?<=.+)\.aspx(?=\?.+)?", RegexOptions.IgnoreCase); //匹配最初的.aspx请求 Match math1 = reg1.Match(Context.Request.RawUrl); if (math1.Success) { DictionaryClass.MachingUrl(Context); Context.Response.Redirect(DictionaryClass.Str_FakePath); //从最初的.aspx重定向到对应的.html } } } }
<system.web> <httpModules> <add name="MyHttpModule" type="MyModule.UrlReWriteHttpModule"/> </httpModules> <httpHandlers> <add verb="*" path="*.html" type="UrlRewriter"/> </httpHandlers> ...... </system.web>
<httpModules> <add name="UrlReWriteHttpModule" type="MyModule.UrlReWriteHttpModule,App_Code"/> </httpModules>