代码改变世界

ASP.NET通过IHttpModule实现伪静态

2011-10-17 12:17  张剑  阅读(303)  评论(0编辑  收藏  举报

 在ASP.NET中,有很多种实现伪静态的办法,我们今天主要介绍通过IHttpModule这个接口的实现,来解决问题。

 相对于整个应用程序来讲,如果我们需要在请求发生之时对请求的地址进行处理就需要用到IHttpModule接口。常用实现伪静态技术。就是将一个Get访问的查询字符串变成一个独立的文件。但是在程序中实际上访问的还是查询字符串中的值。如:

Http://www.cnsaiko.com/news.aspx?id=1

改变为

Http://www. cnsaiko.com/news_1.aspx

这样做的好处在于有利于SEO及防止SQL注入等。当然,文件的扩展名在服务器支持的情况下也是可以变化的。

 

IHttpModule接口中有两个方法供我们实现:

Dispose() 处置由实现 IHttpModule 的模块使用的资源(内存除外)。

Init() 初始化模块,并使其为处理请求做好准备。

第一步:如果要在应用程序中使用IHttpModule ,则需要配制web.config文件。

 

在Web.config文件中,我们需要对httpModules元素进行投置,该节点在system.web元素中。如下:

  1. <httpModules> 
  2.             <add name="HttpModule" type="WebApplication1.HttpModule"/> 
  3. </httpModules> 

注意:其中type,必须为实现了IHttpModule接口的类型的俱体路径(包括命名空间)。

第二步:实现IHttpModule

 

新建一个HttpModule类,与配制文件中的类型命名空间路径相同。

使用该类实现IHttpModule接口。

我们开始的代码将写在Init方法中,该方法中的HttpApplication对象context包括程用程序对象的所有方法,属性和事件。那么在这个对象中,我们就可以开始一个请求事件的处理。

在Init方法中可以书写:

  1. context.BeginRequest += new EventHandler(context_BeginRequest); 

以上代码,为请求开始事件注册了一个委托方法。

在注册委托方法时,可以在写完+=后,连续按两次Tab键,会自动生成context_BeginRequest方法。

第三步:关于context_BeginRequest()方法

 

 

 在context_BeginRequest方法中,我们可根据请求的不同,进行请求的重新定向。这样就可以将虚拟的伪静态路径变成我们程序所需要的Get方式查询字符串。

要想改变请求,就必须得到当前求请,在ASP.NET中,使用HttpContext类型来封装所有请求对象。那么,我们可以通过刚刚的HttpApplication对象来获取HttpContext对象。如:

  1. HttpApplication application = sender as HttpApplication; 
  2. HttpContext context = application.Context; 
第四步:得到请求与重定向
 
(1)得到请求:
可以使用HttpContext对象的属性Request来获取当前请求。
在得到当前请求后,可以使用正则表达式,从请求中获取所需的可变信息,用来重新定向到新的位置。
 
(2)重定向:
可以使用HttpContext对象的方法RewritePath,可以将得到的新路径放到方法参数中。
 
原理清楚后,看下俱体代码实现:
 
 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Text.RegularExpressions; 
  6.  
  7. /// <summary> 
  8. ///HttpModel 的摘要说明 
  9. /// </summary> 
  10. public class HttpModule:IHttpModule 
  11.         public void  Dispose() 
  12.         { 
  13.             throw new NotImplementedException(); 
  14.         } 
  15.  
  16.         public void  Init(HttpApplication context) 
  17.         { 
  18.             context.BeginRequest += new EventHandler(context_BeginRequest);//为BeginRequest事件注册方法 
  19.         } 
  20.  
  21.         void context_BeginRequest(object sender, EventArgs e) 
  22.         { 
  23.             HttpApplication application = (HttpApplication)sender;//得到当前应用程序对象 
  24.             HttpContext context = application.Context; //得到当前请求的上下文 
  25.  
  26.             string url =context.Request.Url.ToString(); //得到当前请求的假URL 
  27.             int lastindex = url.LastIndexOf('/'); //从最后一个/取索引 
  28.             string filename = url.Substring(lastindex);//得到文件名 
  29.             Regex reg = new Regex(@"(\d+)\.aspx"); //创建正则对象,用来验证文件名是否满足条件 
  30.             if (reg.IsMatch(filename)) 
  31.             { 
  32.                 Match match = reg.Match(filename);//创建Match对象 
  33.                 string id = match.Groups[1].Value;//通过正则结果对象,取到组()中数字的值(ID) 
  34.  
  35.                 context.RewritePath("View.aspx?id=" + id);//通过指定的ID,重写真正存在的URL地址。 
  36.             } 
  37.  
  38.         }