天天看点

IHttpModule实现URL重写,页面静态化

步骤如下:

 首先编写自定义IHttpModule实现,这个定义只定义了两个方法Dispose()和Init()。在这里我们可以不用关注Dispose()这个方法,这个方法是用来实现如何最终完成资源的释放的。在Init方法中有一个HttpApplication参数,可以在方法中可以自定义对HttpApplication的事件处理方法。比如这里我们的代码如下:

IHttpModule实现URL重写,页面静态化

public void Init(HttpApplication context)

IHttpModule实现URL重写,页面静态化

{

IHttpModule实现URL重写,页面静态化

                 //context.BeginRequest是开始处理HTTP管线请求时发生的事件

IHttpModule实现URL重写,页面静态化

                 context.BeginRequest += new EventHandler(context_BeginRequest);

IHttpModule实现URL重写,页面静态化

                 //context.Error是当处理过程中发生异常时产生的事件

IHttpModule实现URL重写,页面静态化

                 //context.Error += new EventHandler(context_Error);

IHttpModule实现URL重写,页面静态化

}

 然后在context_BeginRequest这个方法中自己编写处理事件,我们编写的代码如下:

IHttpModule实现URL重写,页面静态化

void context_BeginRequest( object sender, EventArgs e)

IHttpModule实现URL重写,页面静态化

{

IHttpModule实现URL重写,页面静态化

                 HttpApplication application = (HttpApplication)sender;

IHttpModule实现URL重写,页面静态化

                 HttpContext context = application.Context;

IHttpModule实现URL重写,页面静态化

                 HttpResponse response = context.Response;

IHttpModule实现URL重写,页面静态化

                 string path = context.Request.Path;

IHttpModule实现URL重写,页面静态化

                 string file = System.IO.Path.GetFileName(path);

IHttpModule实现URL重写,页面静态化

                 //重写后的URL地址

IHttpModule实现URL重写,页面静态化

                 Regex regex= new Regex( "UserInfo(\\d+).aspx",RegexOptions.Compiled);

IHttpModule实现URL重写,页面静态化

                 Match match=regex.Match(file);

IHttpModule实现URL重写,页面静态化

                 //如果满足URL地址重写的条件

IHttpModule实现URL重写,页面静态化

                 if(match.Success)

IHttpModule实现URL重写,页面静态化

                 {

IHttpModule实现URL重写,页面静态化

                         string userId = match.Groups[1].Value;

IHttpModule实现URL重写,页面静态化

                         string rewritePath = "UserInfo.aspx?UserId=" + userId;

IHttpModule实现URL重写,页面静态化

                         //将其按照UserInfo.aspx?UserId=123这样的形式重写,确保能正常执行

IHttpModule实现URL重写,页面静态化

                         context.RewritePath(rewritePath);

IHttpModule实现URL重写,页面静态化

                 }

IHttpModule实现URL重写,页面静态化

}

 注意在上面的代码中采用了正则表达式来进行匹配,使用正则表达式的好处就是在处理格式化文本时相当灵活。除此之外,我们在处理方法中仅仅对满足要求的URL进行重写,对于不满足要求的URL则无需进行重写,所以这样就不会干扰没有重写的URL的正常运行(比如Index.aspx)。

 从那段从《ASP.NET夜话》摘出的话中可以看出,仅仅是编写自己的IHttpModule实现还是不够的,我们还需要让处理Web请求的程序直到我们编写的IHttpModule实现的存在,这就需要在web.config中配置。在本实例中只需要在本ASP.NET项目中的web.config节点中增加一个<httpModules></httpModules>节点(如果已存在此节点则可以不用添加),然后在此节点中增加一个配置即可,对于本实例,这个节点最终内容如下: < httpModules >

< add name ="UrlReWriter" type ="UrlReWriter" />

</ httpModules >

 这样我们的URL地址重写就成功了。

 下面一个演示显示用户列表的链接的页面设计代码: < %@ Page Language ="C#" AutoEventWireup ="true"     CodeFile ="Default.aspx.cs" Inherits ="_Default" % >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html xmlns ="http://www.w3.org/1999/xhtml" >

< head runat ="server" >

         < title >无标题页 </title>

</head>

< body >

         < form id ="form1" runat ="server" >

         < div >

         < ol >

         < li > < a href ="UserInfo1.aspx" >编号为1的用户信息 </a> </li>

         < li > < a href ="UserInfo2.aspx" >编号为2的用户信息 </a> </li>

         < li > < a href ="UserInfo3.aspx" >编号为3的用户信息 </a> </li>

         < li > < a href ="UserInfo4.aspx" >编号为4的用户信息 </a> </li>

         < li > < a href ="UserInfo.aspx?UserId=5" >编号为5的用户信息 </a> </li>

         </ol>

         </div>

         </form>

</body>

</html>

  在UserInfo.aspx页面的设计代码如下: < %@ Page Language ="C#" AutoEventWireup ="true" CodeFile ="UserInfo.aspx.cs" Inherits ="UserInfo" % >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html xmlns ="http://www.w3.org/1999/xhtml" >

< head runat ="server" >

         < title >无标题页 </title>

</head>

< body >

         < form id ="form1" runat ="server" >

         < div >

                 < asp:Literal ID ="Literal1" runat ="server" > </asp:Literal>

                 < br />

         < img src ="images/13.jpg" width ="283" height ="485" title ="MM" />

         </div>

         </form>

</body>

</html>

  UserInfo.aspx页面的对应逻辑代码如下:

IHttpModule实现URL重写,页面静态化

using System;

IHttpModule实现URL重写,页面静态化

using System.Collections;

IHttpModule实现URL重写,页面静态化

using System.Configuration;

IHttpModule实现URL重写,页面静态化

using System.Data;

IHttpModule实现URL重写,页面静态化

using System.Web;

IHttpModule实现URL重写,页面静态化

using System.Web.Security;

IHttpModule实现URL重写,页面静态化

using System.Web.UI;

IHttpModule实现URL重写,页面静态化

using System.Web.UI.HtmlControls;

IHttpModule实现URL重写,页面静态化

using System.Web.UI.WebControls;

IHttpModule实现URL重写,页面静态化

using System.Web.UI.WebControls.WebParts;

IHttpModule实现URL重写,页面静态化
IHttpModule实现URL重写,页面静态化

public partial class UserInfo : System.Web.UI.Page

IHttpModule实现URL重写,页面静态化

{

IHttpModule实现URL重写,页面静态化

         protected void Page_Load( object sender, EventArgs e)

IHttpModule实现URL重写,页面静态化

         {

IHttpModule实现URL重写,页面静态化

                 if (!Page.IsPostBack)

IHttpModule实现URL重写,页面静态化

                 {

IHttpModule实现URL重写,页面静态化

                         string queryString = Request.QueryString[ "UserId"];

IHttpModule实现URL重写,页面静态化

                         int userId=0;

IHttpModule实现URL重写,页面静态化

                         if ( int.TryParse(queryString, out userId))

IHttpModule实现URL重写,页面静态化

                         {

IHttpModule实现URL重写,页面静态化

                                 Literal1.Text = "这是编号为" + userId.ToString() + "的用户的信息。";

IHttpModule实现URL重写,页面静态化

                         }

IHttpModule实现URL重写,页面静态化

                         else

IHttpModule实现URL重写,页面静态化

                         {

IHttpModule实现URL重写,页面静态化

                                 Literal1.Text = "错误的参数";

IHttpModule实现URL重写,页面静态化

                         }

IHttpModule实现URL重写,页面静态化

                 }

IHttpModule实现URL重写,页面静态化

         }

IHttpModule实现URL重写,页面静态化

} 尽管我们进行了URL地址重写并不会页面的最终显示效果,因为最终实际执行的还是UserInfo.aspx?UserId=数字这种形式的URL。

         如果有兴趣还可以对全站进行伪静态化处理,所谓的伪静态化就是所有的链接在浏览器地址栏里看到的都是.html这样的形式,实际上执行的仍是动态页面,不过这就需要对IIS进行配置才行。 转载于 http://zhoufoxcn.blog.51cto.com/792419/177841

转载于:https://www.cnblogs.com/alern/archive/2012/11/14/2769203.html