URL重寫之實現IHttpHandler接口 .net 2.0實現 (幻想曲作品)

以前用url重寫時是用的ms urlrewriter,用了以後發現了很多不足,自定義功能太弱,而且隨着重寫規則的增加,web.config可能會越來越大,實際上,url重寫就是實現IHttpHandler接口.
整個流程分二步走:
1、用一個xml文件來存儲重寫規則,其中這些規則是一些簡單的正則表達式
2、實現IHttpHandler接口
首先看一下xml文件的格式:
code 程序代碼

<?xml version="1.0" encoding="utf-8" ?>
<root>
<regex>
                <!--重寫以後的虛擬地址-->
<b><![CDATA[xxx,(?<id>[0-9]+).html$]]></b>
<!--實際地址-->
                <a><![CDATA[xxx.aspx?id=${id}]]></a>        
</regex>
</root>

相信上面的xml大家都能看懂.
code 程序代碼

using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;
//RegexInfo結構,用來存儲從xml文件中讀取到的數據
public struct RegexInfo
{
    public string _before;
    public string _after;
    public RegexInfo(string before, string after)
    {
        _before = before.ToLower();
        _after = after.ToLower();
    }
}
//ipFilter結構,用來存儲被封的IP
public struct ipFilter
{
    public string _ip;
    public ipFilter(string ip)
    {
        _ip = ip;
    }
}
public class HtmlHttpHandler : IHttpHandler   //實現IHttpHandler接口
{

    private List<RegexInfo> _regex_list = new List<RegexInfo>();
    private List<ipFilter> _ip_filter = new List<ipFilter>();
    public HtmlHttpHandler()
    {
        DataSet ds = new DataSet();
        //讀取url重寫規則文件,並寫入RegexInfo結構的實例中
        ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Regexs.xml"));
        foreach (DataRow r in ds.Tables["regex"].Rows)
            _regex_list.Add(new RegexInfo(((string)r["b"]).Trim(), ((string)r["a"]).Trim()));
        ds.Reset();
        //讀取被封的IP列表
        ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/ipFilter.xml"));
        foreach(DataRow r in ds.Tables["IpFilters"].Rows)
            _ip_filter.Add(new ipFilter((string)r["ip"]));
    }

    public void ProcessRequest(HttpContext context)
    {
        string _ip = context.Request.UserHostAddress;   //獲取IP
        foreach (ipFilter r in _ip_filter)
        {
            if (_ip == r._ip)
            {
                context.Response.Write("對不起,您的IP:"+_ip+"已被限制!");
                context.Response.End();
            }
        }
        string path = context.Request.Path.ToLower();   //獲取當前訪問的重寫過的虛假URL
        foreach (RegexInfo r in _regex_list)
            path = Regex.Replace(path, r._before, r._after);      //匹配出其真實的URL
        context.Server.Execute(path); 
    }

    // Override the IsReusable property.
    public bool IsReusable
    {
        get { return true; }
    }
}

OK,IHttpHandler接口就被實現了,下面稍配一下web.config就可以實現URL重寫了
在web.config的<system.web></system.web>中加入
code 程序代碼

 <httpHandlers>
      <add verb="*" path="*.html" type="HtmlHttpHandler"/>
 </httpHandlers>


表示後綴名爲.html的文件全部交給HtmlhttpHandler類去處理
最後配一下iis就行了
本blog的url重寫就是這樣實現的,至於簡繁的轉換,你可以加到ProcessRequest中,至於如何實現轉換,到這個blog上找找吧:)  

http://www.lemongtree.com/zh-cn/item,381.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章