1.新建MyModule.cs继承IHttpModule,实现BeginRequest事件。
public class MyModule:IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += CheckPage; } private void CheckPage(object sender, EventArgs e) { HttpApplication app = sender as HttpApplication; string urlStr = app.Request.RawUrl; //获取原始地址url Good_1.html Regex regex = new Regex(@"w+_+d+.html"); //正则匹配,英文+下划线+数字+.html类型 if (regex.IsMatch(urlStr)) { int line = urlStr.IndexOf('_'); int dot = urlStr.IndexOf('.'); string id = urlStr.Substring(line + 1, dot - line - 1); //找出页面跳转的Id string directUrl = "~/Good_list.aspx?id=" + id; //拼接成动态网页,加上Id app.Server.Transfer(directUrl); //重定向新的网址 } } public void Dispose() { throw new NotImplementedException(); } }
2.在Web.config配置中注册httpModule
<system.webServer> <modules> <add name="myModule" type="WebApplication7.MyModule"/> </modules> </system.webServer>
3.建立Good_list.aspx页面,在cs方法内调用
public partial class Good_list : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string id = Context.Request["id"]; Context.Response.Write("这是" + id + "商品"); } } }