采用HttpModules来重写URLs(实践篇)
来源:百度文库 编辑:神马文学网 时间:2024/04/29 00:50:59
首先写一个处理URLs重写的类,并且这个类必须继承IHttpHandler接口,以博客园的程序为例:
public class UrlReWriteModule : System.Web.IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest +=new EventHandler(context_BeginRequest);
}
public void Dispose()
{
}
}
UrlReWriteModule类就是处理URLs重写的类,继承IHttpHandler接口,实现该接口的两个方法,Init和Dispose。在Init方法里注册自己定义的方法,如上例所示:
content.BeginRequest +=new EventHandler(content_BeginRequest);
BeginRequest是一个事件,在收到新的Http请求时触发,content_BeginRequest就是触发时处理的方法。另外说明一点,HttpModules能注册的方法还有很多,如:EndRequest、Error、Disposed、PreSendRequestContent等等。
在content_BeginRequest方法中具体处理URLs重写的细节,比如,将http://www.cnblogs.com/rrooyy/archive/2004/10/24/56041.html 重写为http://www.cnblogs.com/archive.aspx?user=rrooyy&id=56041 (注:我没有仔细看DuDu的程序,这里只是举例而已)。然后将重新生成的Url用HttpContext.RewritePath()方法重写即可,如下:
private void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
// 获取旧的Url
string url = context.Request.Path.ToLower();
// 重新生成新的Url
string newUrl = ...; // 具体过程略
// 重写Url
context.RewritePath(newUrl);
}
提醒:newUrl的格式不是http://www.infotouch.com/user/archive.aspx,而是从当前应用程序根目录算起的绝对路径,如:user\archive.aspx,这一点请特别注意。
最后要web.config中注册重写URLs的类,格式如下:
采用标签可以注册一个类;可以移除某个类,如果某个子目录不希望继承父目录的某个Http Module注册,就需要使用这个标签; 可以移除所有的Http Module注册。
public class UrlReWriteModule : System.Web.IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest +=new EventHandler(context_BeginRequest);
}
public void Dispose()
{
}
}
UrlReWriteModule类就是处理URLs重写的类,继承IHttpHandler接口,实现该接口的两个方法,Init和Dispose。在Init方法里注册自己定义的方法,如上例所示:
content.BeginRequest +=new EventHandler(content_BeginRequest);
BeginRequest是一个事件,在收到新的Http请求时触发,content_BeginRequest就是触发时处理的方法。另外说明一点,HttpModules能注册的方法还有很多,如:EndRequest、Error、Disposed、PreSendRequestContent等等。
在content_BeginRequest方法中具体处理URLs重写的细节,比如,将http://www.cnblogs.com/rrooyy/archive/2004/10/24/56041.html 重写为http://www.cnblogs.com/archive.aspx?user=rrooyy&id=56041 (注:我没有仔细看DuDu的程序,这里只是举例而已)。然后将重新生成的Url用HttpContext.RewritePath()方法重写即可,如下:
private void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
// 获取旧的Url
string url = context.Request.Path.ToLower();
// 重新生成新的Url
string newUrl = ...; // 具体过程略
// 重写Url
context.RewritePath(newUrl);
}
提醒:newUrl的格式不是http://www.infotouch.com/user/archive.aspx,而是从当前应用程序根目录算起的绝对路径,如:user\archive.aspx,这一点请特别注意。
最后要web.config中注册重写URLs的类,格式如下:
采用
采用HttpModules来重写URLs(实践篇)
重写戒淫证辑(2)
Nutch 笔记(二):Craw more urls and Recrawl
采用[ICONIX] 方法实践BLOG设计之四 [健壮性分析]
采用[ICONIX] 方法实践BLOG设计之四 [健壮性分析]
Creating Vanity URLs
Tapestry4 Friendly URLs
10 Worst Company URLS
9. Working with URLs
我来实践天枢穴
:慈善机构采用私营部门的模式来筹集资金 - Knowledge@Whar...
人机界面设计(实践篇)
用实践检验“真理”,用什么来检验“实践”?
用实践检验“真理”,用什么来检验“实践”? - 杨恒均
用实践检验“真理”,用什么来检验“实践”?
用实践检验“真理”,用什么来检验“实践
著名诗人牛汉:现代文学史要重写(图)
重写中国近代史的期待(《告别革命》片断?)
重写中国近代史的期待(《告别革命》片断?)
朱元璋是高丽人? 韩国网友要求重写历史(图)
重写中国近代史的期待(《告别革命》片断?)
著名诗人牛汉:现代文学史要重写(图)
重写戒淫证辑
重写戒淫证辑