All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
c#,md5,加密函数_[Asp.Net教程]
PHP一些常用的正则表达式_php资料_编程技术
PHP进阶教程:实现网站的无限分类_php资料_编程技术
SQL循序渐进(14)组合条件和布尔运算符_[SQL,Server教程]
.net发送邮件的一些技巧_.net资料_编程技术
如何实现SQL Server 2005快速web分页_[SQL Server教程]
PHP实例:PHP生成带有雪花背景的网站验证码_php资料_编程技术
C#教程:使用Visual Studio 2005 创建项目
Asp.Net图片验证码程序[含源码]_[Asp.Net教程]
javascript在asp.ne中的应用_[Asp.Net教程]
Reading number is top 10 pictures
The real super beauty8
青涩甜美-王祖贤小时候的旧照片曝光
Sora aoi mirror memorial classics3
Sora aoi mirror memorial classics1
俄罗斯台球天后惊艳魅惑1
The hot big eye big breast beauty3
China's programmers are live what kind, had a look at will know that
Sora aoi calligraphy show
人美胸美腿更美2
教你22句话
Download software ranking
Visual C++界面编程技术
中国结婚习俗实录
C语言教程TXT
Unix video tutorial18
美女写真3
功夫熊猫2(下集)
Detective task-the top secret prostitution files
Be there or be square
双旗镇刀客B
Tram sex maniac 2 (H) rar bag1
delv published in(发表于) 2014/1/6 9:07:33 Edit(编辑)
URL重写实现IHttpHandler接口_[Asp.Net教程]

URL重写实现IHttpHandler接口_[Asp.Net教程]

URL重写实现IHttpHandler接口_[Asp.Net教程]

以前用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就行了。


至于简繁的转换,你可以加到ProcessRequest中,至于如何实现转换见下一页。


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.