【转】 用 IHttpHandler 实现url重写
2019-08-04 06:42
2001 查看
原文链接:https://www.geek-share.com/detail/2361095540.html
以前用url重写时是用的ms urlrewriter,用了以后发现了很多不足,自定义功能太弱,而且随着重写规则的增加,web.config可能会越来越大,实际上,url重写就是实现IHttpHandler接口.
整个流程分二步走:
1、用一个xml文件来存储重写规则,其中这些规则是一些简单的正则表达式
2、实现IHttpHandler接口
首先看一下xml文件的格式:
程序代码
<?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大家都能看懂.
程序代码
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>中加入
程序代码
<httpHandlers>
<add verb="*" path="*.html" type="HtmlHttpHandler"/>
</httpHandlers>
表示后缀名为.html的文件全部交给HtmlhttpHandler类去处理
最后配一下iis就行了
整个流程分二步走:
1、用一个xml文件来存储重写规则,其中这些规则是一些简单的正则表达式
2、实现IHttpHandler接口
首先看一下xml文件的格式:
程序代码
<?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大家都能看懂.
程序代码
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>中加入
程序代码
<httpHandlers>
<add verb="*" path="*.html" type="HtmlHttpHandler"/>
</httpHandlers>
表示后缀名为.html的文件全部交给HtmlhttpHandler类去处理
最后配一下iis就行了
转载于:https://www.cnblogs.com/gaohades/archive/2006/06/12/423916.html
相关文章推荐
- ASP.NET URL重写实现IHttpHandler接口
- ASP.net:URL重写实现IHttpHandler接口
- URL重写实现IHttpHandler接口---网站的简、繁转换
- ihttphandler 实现url重写 (asp.net C#)
- 【转】 用 IHttpHandler 实现url重写
- 【IHttpHandler】IHttpModule实现URL重写
- ASP.NET URL重写实现IHttpHandler接口
- URL重写实现IHttpHandler接口(Asp.Net C#)
- 用自定义IHttpModule实现URL重写
- .net实现URL重写
- ASP.net实现无扩展名的URL重写
- ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置
- UrlRewritingNet 完美实现 ASP.NET 2.0 中的URL重写(映射)
- apache url重写实现伪静态
- 如何实现一个通用的IHttpHandler 万能的IHttpHandler HttpWebRequest文件上传
- ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置
- Urlriter实现URL重写
- 使用HttpHandler实现URL重写
- Asp.net中创建程序集以及一个简单的IHttpHandler 实现
- IIS通过URL重写实现防盗链