首页 > 编程 > .NET > 正文

ASP.NET中的URL过滤实现代码

2024-07-10 12:41:02
字体:
来源:转载
供稿:网友

下面是类的定义。
代码如下:
using System;
 using System.Web;
 using System.Web.SessionState;

 namespace QTJZ
 {
     public class Filters : IHttpModule, IRequiresSessionState
     {
         public void Dispose() { }

         public void Init(HttpApplication application)
         {
             application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
         }

         public void application_AcquireRequestState(object sender, EventArgs e)
         {
             HttpApplication application = sender as HttpApplication;
             HttpRequest request = application.Request;
             HttpResponse response = application.Response;

             string url=request.CurrentExecutionFilePath.Trim('/');
             string suffix = request.CurrentExecutionFilePathExtension.Trim('.');

             if (!url.Equals("Default.htm") && (suffix == "aspx" || suffix == "htm"))
             {
                 object sessionObj = application.Context.Session == null ? null : application.Session["useID"];
                 if (sessionObj==null)
                 {
                     response.Redirect("~/Default.htm");
                 }
             }
         }
     }
 }

为了实现过滤的效果,Filters类需要实现IHttpMoeld接口,实现该接口需要有两个方法,一个是Dispose,另一个是Init。Init的参数是一个HttpApplication类型的实例,利用这个实例注册一些事件。由于现在要对URL进行过滤,因此注册的是AcquireRequestState事件。类似事件罗列如下

BeginRequest
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表