HttpModule(URL重写 ).html伪静态
利用HttpAplication中的-->
1.BeginRequest(在 ASP.NET 响应请求时作为 HTTP 执行管线链中的第一个事件发生)
来执行RewritePath
简单URL重写实例代码
public class IIHttpMoudle:IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
throw new NotImplementedException();
}
//初始化事件
public void Init(HttpApplication context)
{
//将方法注册到事件上面
context.BeginRequest += MyHttpModult;
}
void MyHttpModult(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string filepath = app.Context.Request.Path;
string filename = System.IO.Path.GetFileName(filepath);
Regex reg = new Regex("View.(\\d+)html");//正则表达式规则
Match match = reg.Match(filename);
if (match.Success)
{
string id = match.Groups[1].Value;//得到id
app.Context.RewritePath("View.aspx?id=" + id);
}
}
#endregion
}
一个完整的HTTP请求在ASP.NET Framework的处理过程如下:
HttpRequest ——> inetinfo.exe ——> ASPNET_ISAPI.dll ——> Http Pipeline ——> ASPNET_WP.exe ——> HttpRuntime ——> HttpApplication Factory ——> HttpApplication ——> HttpModule ——> HttpHandler Factory ——> HttpHandler ——> HttpHandler.ProcessRequest()
HTTP Handler提供了类似于ISAPI Server Extention的功能,而HttpModule实现了类似于ISAPI Filter的功能。使用自定义的Handler会覆盖系统默认的Handler,而Module是可以多个同时存在的。