asp.net webform过滤器(注意我们可以在拦截请求的同时设置回调函数)

 

转载:https://www.cnblogs.com/kexb/p/5923359.html

 

 

复制代码
1.过滤器代码
 public class PageFilter : IHttpModule
    {
        public String ModuleName
        {
            get { return "PageFilter"; }
        }

        public void Dispose()
        {

        }

        //在 Init 方法中注册HttpApplication 
        // 通过委托方式注册事件
        public void Init(HttpApplication application)
        {

            application.BeginRequest += Application_BeginRequest;
            application.Error += Application_Error;
            application.AcquireRequestState += new EventHandler(Application_AcquireRequestState);
            application.EndRequest += Application_EndRequest;
        }

        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            HttpSessionState session = context.Session;
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            String contextPath = request.ApplicationPath;
            var rawUrl = request.RawUrl;
            if (rawUrl.Contains("/admin/Sys/SysDictTypeEdit.aspx") && rawUrl.Contains("_method=Save"))
            {
                System.Diagnostics.Debug.WriteLine("begin request");
            }
          
        }

        private void Application_Error(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            HttpSessionState session = context.Session;
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            String contextPath = request.ApplicationPath;
            var rawUrl = request.RawUrl;
            if (rawUrl.Contains("/admin/Sys/SysDictTypeEdit.aspx") && rawUrl.Contains("_method=Save"))
            {
                System.Diagnostics.Debug.WriteLine("error");
            }
        }

        private void Application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            HttpSessionState session = context.Session;
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            String contextPath = request.ApplicationPath;
            var rawUrl = request.RawUrl;
            if (rawUrl.Contains("/admin/Sys/SysDictTypeEdit.aspx") && rawUrl.Contains("_method=Save"))
            {
                System.Diagnostics.Debug.WriteLine("end request");
            }
        }

        private void Application_AcquireRequestState(Object source, EventArgs e)
        {
            try
            {
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
                HttpSessionState session = context.Session;
                HttpRequest request = context.Request;
                HttpResponse response = context.Response;
                String contextPath = request.ApplicationPath;
                if (application.Context.Handler is System.Web.UI.TemplateControl)
                {
                    var path = ((System.Web.UI.TemplateControl)application.Context.Handler).AppRelativeVirtualPath;
                    if (path == "~/admin/Sys/SysDictTypeEdit.aspx" && request["_method"] == "Save")
                    {
                        Action action = () => System.Diagnostics.Debug.WriteLine("Save777");
                        //注意可以在这里往上下文的IDcitionary里放委托,将来可用于回调(可理解为注册)
                        context.Items.Add(request["_method"], action);
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
2.web.config配置
<system.web>
  <httpModules>
      <add name="pageModule" type="HraWeb.PageFilter,HraWeb"/>
  </httpModules>
</system.web>
<system.webserver>
 <modules>
      <add name="pageModule" type="HraWeb.PageFilter,HraWeb"/>
 </modules>
</system.webserver>
复制代码

 

posted @ 2019-11-26 11:49  DarJeely  阅读(624)  评论(0编辑  收藏  举报