標籤:style blog http color io os 使用 ar 檔案
1. 修改設定檔:
<httpModules> <</span>add name="html" type="HttpModule"/></httpModules>
2. 編寫HttpModule類:
using System;using System.Collections.Generic;using System.Linq;using System.Web;///URL重寫 的摘要說明///1.建立類檔案 HttpModule.cs///2.實現IHttpModule介面的方法public class HttpModule : IHttpModule{ //清理資源 public HttpModule(){} public void Dispose(){} /// /// 初始化 /// /// public void Init(HttpApplication context) { //3.在初始化方法中添加事件處理方法 context.BeginRequest += new EventHandler(BeginRequest); context.EndRequest += new EventHandler(EndRequest); } //4. 編寫開始處理請求代碼 public void BeginRequest(object sender, EventArgs e) { HttpApplication app = sender as HttpApplication; //app.Response.Write("開始處理請求!"); //4.1 截獲請求網址 string url = app.Request.RawUrl; //4.2 判斷網址是否符合要求,判斷是否進行重發到指定頁面 if (url.LastIndexOf(".html") > 0) { //轉寄到指定頁面 app.Server.Transfer("~/HttpMoudle/UrlReWrite.aspx"); } } public void EndRequest(object sender, EventArgs e) { HttpApplication app = sender as HttpApplication; //app.Response.Write("結束處理請求!"); } }
3.再次訪問.html結尾的網址是,就會跳轉到指定的頁面,當然,也可以截取字串,僅僅把.aspx替換為.html更好!
使用HttpModule實現網址重寫