(简单)方法一:
1.首先在网站的根目录下添加一个Global.asax,全局的应用程序
2.找到Application_BeginRequest
1)获取应用程序的路径将其变为相对路径
string url = Request.AppRelativeCurrentExecutionFilePath;
2)用正则表达式匹配获得的路径
Match match = Regex.Match(url, @"~/BookList/BookList_(\d+).aspx");
3)用Context.RewritePath跳转到对应的程序目录
if (match.Success) { string categoryId=match.Groups[1].Value; Context.RewritePath("/BookList/BookList.aspx?categoryId=" + categoryId); }
方法二:
第一步:新建一个类,实现IHttpModule接口
第二步:在Init的方法里边注册context.BeginRequest的事件
第三步:写注册事件的context_BeginRequest的方法。
void context_BeginRequest(object sender, EventArgs e) { HttpApplication app = sender as HttpApplication; string url = app.Request.RawUrl;//请求的路径 Regex regex = new Regex(@"index\-(\d+)\.html"); Match match = regex.Match(url); if (match.Success) { int id = int.Parse(match.Groups[1].Value); app.Context.RewritePath("Index.aspx?id="+id); } }
第四步:在Web.config里边配置。
<system.web> <httpModules> <add name="urlrewrite" type="WebUrlRewrite.UrlRewrite"/>//命名控件加类名 </httpModules> <system.web>