首先我们新建一个类库命名为Module,然后新建一个类命名为UrlRewriteModule 并且实现IHttpHandler接口代码如下

public class UrlRewriteModule:IHttpModule
    {

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            throw new NotImplementedException();
        }
    }

先别管Dispose方法 咱看下Init方法 重命名上来看是不是就是初始化的意思啊! 然后看它的参数HttpApplication 想想 咱有这个东西了是不是就能做点什么事了嘿嘿,废话少说先输出个HelloWorld吧!!!好转到HttpApplication的定义,是不是看到有个BeginRequest的事件 public event EventHandler BeginRequest;

从单词上来看这个事件是不是应用程序以开始最先执行的事件吧 对没错,好下面咱就实现应用程序一开始执行就向页面中输入一行文字 hello  world

 

public class UrlRewriteModule:IHttpModule
    {

        public void Dispose()
        {
            throw newNotImplementedException();
        }
       public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
 
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            app.Response.Write("hello world");
        }
 }

那下一步干什么呢?咱只定义了这么个类怎么让应用程序知道它的存在呢?好下面打开web.config文件然后加入如下代码注意在system.web节点中

 <httpModules>
      <add name="UrlRewriteModule" type="Moduel.UrlRewriteModule"/>
    </httpModules>

第一个参数name 就是刚才定义的那个类名。第二个参数type就是程序集名加类名,好大功告成

行大概步骤知道了咱就实现下URL重新的功能吧!

需求:把原始的HomeDetail.aspx?Id=1 重写成HomeDetail-1.html 是不是就是伪静态啊 ! 差不多吧 呵呵 下面看下代码关键性的几行代码

void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            //app.Context.Response.Write("hahahahaa~~~");
            string requestPath = app.Context.Request.FilePath;
            string fileName = System.IO.Path.GetFileName(requestPath);
            Regex reg = new Regex("HomeDetail-(\\d+).html");
            Match m = reg.Match(fileName);
            if (m.Success)
            {
                string id = m.Groups[1].Value;
                app.Context.RewritePath("HomeDetail.aspx?Id=" + id);
            }
            //app.Context.Response.Write(fileName);
        }

首先获得当前请求的虚拟路径,然后获得具体的文件名称,通过正则表达式把Id提取出来最后重写路径。
HomeList.aspx 页面做如下处理

             <!-- 原始是转向地址 -->
                    <a href = "HomeDetail.aspx?Id=<%# Eval("Id") %>"><%#Eval("Name") %></a>
                    <!-- 重写后的地址地址 -->
                    <a href = "HomeDetail-<%# Eval("Id") %>.html"><%#Eval("Name") %></a>

下面运行一下

 ok 搞定,注意:关于url重写,微软已经为我们开发了一个组件urlrewrite 同过这个组件只需要简单的配置一下配置文件就ok了,感兴趣的可以搜一下。

感觉还没过瘾是吧 好下面再整个登陆验证的东东
以前我们做是否登陆验证 就是在每个需要验证的页面的page_load中都要判断session的值如果为空就转向登陆界面如果不为空继续执行对不,好今天咱就来写个Module来实现这么个功能让其全应用程序有效,通过查找资料发现在HttpApplication类的17个事件中只有两个事件能操作到session

1. AcquireRequestState 2. PostRequestHandlerExecute  今天呢咱就用第一种

首先在Module类库里新建一个类SessionModule写这么几行代码

 

 1 class SessionModule:IHttpModule
 2     {
 3 
 4         public void Init(HttpApplication context)
 5         {
 6             context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
 7         }
 8 
 9         void context_AcquireRequestState(object sender, EventArgs e)
10         {
11             HttpApplication app = sender as HttpApplication;
12             string requestPath = app.Request.Path;
13             string fileName = System.IO.Path.GetFileName(requestPath);
14             if (app.Context.Session["userInfo"] == null && ! fileName.Equals("login.aspx"))
15             {
16                 app.Response.Redirect("login.aspx");
17             }
18         }
19 
20 
21     
22 
23         public void Dispose()
24         {
25             throw new NotImplementedException();
26         }
27     }


14 行 判断session 是否为空 以及请求的页面是否为不是登陆界面 如果都成立的话就转向login.aspx 页面中去就这么简单

在web.config 文件中加入一下代码

    <httpModules>
      <add name="UrlRewriteModule" type="Moduel.UrlRewriteModule"/>
      <add name="SessionModule" type="Moduel.SessionModule"/>
    </httpModules>

ok 运行一下 直接进HomeList.aspx 页面

 

ok 大功告成 嘿嘿

最后附上HttpAppliction 中的事件执行循序 需要的时候可以查一下

1. BeginRequest(在 ASP.NET 响应请求时作为 HTTP 执行管线链中的第一个事件发生)本文用到的
2. AuthenticateRequest (当安全模块已建立用户标识时发生。注:AuthenticateRequest 事件发出信号表示配置的身份验证机制已对当前请求进行了身份验证。预订  AuthenticateRequest 事件可确保在处理附加的模块或事件处理程序之前对请求进行身份验证。)
3. PostAuthenticateRequest (注意:该事件在 .NET Framework 2.0 版中是新增的。 当安全模块已建立用户标识时发生。PostAuthenticateRequest 事件在 AuthenticateRequest 事件发生之后引发。预订 PostAuthenticateRequest 事件的功能可以访问由 PostAuthenticateRequest 处理的任何数据。)
4. AuthorizeRequest (当安全模块已验证用户授权时发生。AuthorizeRequest 事件发出信号表示 ASP.NET 已对当前请求进行了授权。预订 AuthorizeRequest 事件可确保在处理附加的模块或事件处理程序之前对请求进行身份验证和授权。)
5. PostAuthorizeRequest (.NET 2.0里新增的事件。在当前请求的用户已获授权时发生。PostAuthorizeRequest 事件发出信号表示 ASP.NET 已对当前请求进行了授权。预订PostAuthorizeRequest 事件可确保在处理附加的模块或处理程序之前对请求进行身份验证和授权。)
6. ResolveRequestCache (当 ASP.NET 完成授权事件以使缓存模块从缓存中为请求提供服务时发生,从而跳过事件处理程序(例如某个页或 XML Web services)的执行。)
7. PostResolveRequestCache (在 ASP.NET 跳过当前事件处理程序的执行并允许缓存模块满足来自缓存的请求时发生。)在 PostResolveRequestCache 事件之后、PostMapRequestHandler 事件之前创建一个事件处理程序(对应于请求 URL 的页)。
8. PostMapRequestHandler(在 ASP.NET 已将当前请求映射到相应的事件处理程序时发生。)
9. AcquireRequestState

10. PostAcquireRequestState
11. PreRequestHandlerExecute(执行事件处理程序。)
12. PostRequestHandlerExecute 注意:能使用到Session
13. ReleaseRequestState
14. PostReleaseRequestState (在PostReleaseRequestState 事件之后,响应筛选器(如果有)将对输出进行筛选。)
15. UpdateRequestCache
16. PostUpdateRequestCache
17. EndRequest

 

 

 

 posted on 2012-06-22 10:30  WlitSoft  阅读(2354)  评论(4编辑  收藏  举报