• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
转:ASP.NET完美操作cookies发布时间:2022-07-10
下一篇:
Asp.netWeb.Config-配置元素httpCookies发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap