在web.config配置文件<system.web> </system.web>中寫代碼 (在2.0版本中)(在3.5版本中有配置文件只需把<add />中的內容換掉)
<httpModules > <add name="httpmodule" type="HttpModule"/> </httpModules>
在App_Code文件中創建HttpModule 類文件 然后繼承接口 IHttpModule 并實現接口
public class HttpModule:IHttpModule{ public void Dispose() { throw new NotImplementedException(); } public void Init(Httpapplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); //在asp.net響應請求時的第一個事件 } // 地址2: http://localhost:3087/weijingtai/1.aspx //偽靜態頁面 // 地址1: http://localhost:3087/weijingtai/News.aspx?id=1 //此事件的作用是: //將連接地址 1.aspx 變為 News.aspx?id=1 void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = sender as HttpApplication; HttpContext context = application.Context; //獲取當前請求的http信息 string filepath = context.Request.RawUrl; //獲取當前請求的原始url int i = filepath.LastIndexOf('/'); //得到文件名開始的索引 string fileName = filepath.Substring(i); Regex rg = new Regex(@"/(/d+).aspx"); //創建正則表達式 if (rg.IsMatch(fileName)) { Match match = rg.Match(fileName); string id = match.Groups[1].Value; //獲取文件名 context.RewritePath("News.aspx?id="+id); //拼接地址 } }}
例子:<li><a href="News.aspx?id=1">這是第一條新聞</a></li> 連接1<li><a href="1.aspx">這是第一條新聞</a></li> 連接2連接1 用 地址1 請求響應 在響應前經過context_BeginRequest事件 由于不匹配正則表達式 所以事件沒起作用 然后響應頁面做出回應 if (Request.QueryString["id"] != null) { Response.Write("這是第" + Request.QueryString["id"] + "條新聞"); }回發給請求頁面連接2 用 地址2 請求響應 在響應前經過context_BeginRequest事件 由于匹配正則表達式 所以將地址2變為地址1 然后響應頁面做出回應 if (Request.QueryString["id"] != null) { Response.Write("這是第" + Request.QueryString["id"] + "條新聞"); }回發給請求頁面
|
新聞熱點
疑難解答