ASP.NET通过IHttpModule实现伪静态

在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元素中。如下:

  • <httpModules>
  •             <add
    name="HttpModule"
    type="WebApplication1.HttpModule"/>
  • </httpModules>

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

新建一个HttpModule类,与配制文件中的类型命名空间路径相同。
使用该类实现IHttpModule接口。
我们开始的代码将写在Init方法中,该方法中的HttpApplication对象context包括程用程序对象的所有方法,属性和事件。那么在这个对象中,我们就可以开始一个请求事件的处理。
在Init方法中可以书写:

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

以上代码,为请求开始事件注册了一个委托方法。
在注册委托方法时,可以在写完+=后,连续按两次Tab键,会自动生成context_BeginRequest方法。
第三步:关于context_BeginRequest()方法


在context_BeginRequest方法中,我们可根据请求的不同,进行请求的重新定向。这样就可以将虚拟的伪静态路径变成我们程序所需要的Get方式查询字符串。
要想改变请求,就必须得到当前求请,在ASP.NET中,使用HttpContext类型来封装所有请求对象。那么,我们可以通过刚刚的HttpApplication对象来获取HttpContext对象。如:

  • HttpApplication application = sender asHttpApplication;
  • HttpContext context = application.Context;

第四步:得到请求与重定向

(1)得到请求:
可以使用HttpContext对象的属性Request来获取当前请求。
在得到当前请求后,可以使用正则表达式,从请求中获取所需的可变信息,用来重新定向到新的位置。

(2)重定向:
可以使用HttpContext对象的方法RewritePath,可以将得到的新路径放到方法参数中。

原理清楚后,看下俱体代码实现:


  • usingSystem;
  • usingSystem.Collections.Generic;
  • usingSystem.Linq;
  • usingSystem.Web;
  • usingSystem.Text.RegularExpressions;
  • /// <summary>
  • ///HttpModel 的摘要说明
  • /// </summary>
  • public
    classHttpModule:IHttpModule
  • {
  •         public
    void  Dispose()
  •         {
  •             throw
    newNotImplementedException();
  •         }
  •         public
    void  Init(HttpApplication context)
  •         {
  •             context.BeginRequest += newEventHandler(context_BeginRequest);//为BeginRequest事件注册方法
  •         }
  •         void context_BeginRequest(objectsender, EventArgs e)
  •         {
  •             HttpApplication application = (HttpApplication)sender;//得到当前应用程序对象
  •             HttpContext context = application.Context; //得到当前请求的上下文
  •             stringurl =context.Request.Url.ToString(); //得到当前请求的假URL
  •             intlastindex = url.LastIndexOf('/'); //从最后一个/取索引
  •             stringfilename = url.Substring(lastindex);//得到文件名
  •             Regex reg = newRegex(@"(\d+)\.aspx"); //创建正则对象,用来验证文件名是否满足条件
  •             if(reg.IsMatch(filename))
  •             {
  •                 Match match = reg.Match(filename);//创建Match对象
  •                 stringid = match.Groups[1].Value;//通过正则结果对象,取到组()中数字的值(ID)
  •                 context.RewritePath("View.aspx?id=" + id);//通过指定的ID,重写真正存在的URL地址。
  •             }
  •         }
  • }
关注.NET开发技术,网站开发,应用系统开发http://www.hnhqwl.com
原文地址:https://www.cnblogs.com/nianyuwen/p/2570533.html