使用HttpModule實現網址重寫

來源:互聯網
上載者:User

標籤: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實現網址重寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.