在幻想曲的幫助下,在這個接口基礎上,我改了少少,用asp.net 1.1版實現,與大家分享
代碼如下:
1.新建工程SofteerStudio,代碼如下:
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;
namespace SofteerStudio
{
//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;
}
}
/// <summary>
/// HtmlHttpHandler 的摘要說明。
/// </summary>
public class HtmlHttpHandler: IHttpHandler
{
private IList _regex_list=new ArrayList() ;
private IList _ip_filter=new ArrayList();
public HtmlHttpHandler()
{
DataSet ds = new DataSet();
//讀取url重寫規則文件,並寫入RegexInfo結構的實例中
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/XMLConfig/Config/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("~/XMLConfig/Config/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; }
}
}
}
2.編譯成dll,引用它至你的項目中.
3.更改你的項目web.config
<add verb="*" path="*.html" type="SofteerStudio.HtmlHttpHandler,SofteerStudio"/>
</httpHandlers>
歡迎與大家交流,我的gmail是softeer#gmail.com
附:xml文件的格式:
<root>
<regex>
<!--重寫以後的虛擬地址-->
<b><![CDATA[xxx,(?<id>[0-9]+).html$]]></b>
<!--實際地址-->
<a><![CDATA[xxx.aspx?id=${id}]]></a>
</regex>
</root>