ASP.NET C# 过滤器

来源:互联网 发布:linux制作iso镜像 编辑:程序博客网 时间:2024/06/05 09:15
 using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
public class PageFilter : IHttpModule
{
    public String ModuleName
    {
        get { return "PageFilter"; }
    }
    //在 Init 方法中注册HttpApplication 
    // 通过委托方式注册事件
    public void Init(HttpApplication application)
    {
        application.AcquireRequestState += new EventHandler(Application_AcquireRequestState);
    }
    private void Application_AcquireRequestState(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        HttpSessionState session = context.Session;
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;
        String contextPath = request.ApplicationPath;
    }
    public void Dispose()
    {
    }
}







在 Visual Studio 中,测试 IHttpModule(httpModules) 正常,但是放到服务器上去就不起作用了,这多半得多服务器 IIS 配置入手。

一、看“应用程序池”的“托管管道模型”是否正确

要使用“集成”才正确,网上有说得用“经典”模式,但我测试得用“集成”模式。

二、web.config 配置是否适合 IIS 版本

在 IIS 7 以下的版本中,应用以下配置:

在 IIS 7 以下的版本中,应用以下配置:

.web>
  >
     name="Cftea.MyHttpModule" type="CfteaHttpModule程序集" />
  >
.web>

在 IIS 7 及以上的版本中,应用以下配置:

.webServer>
  >
     name="Cftea.MyHttpModule" type="CfteaHttpModule程序集" />
  >
.webServer>

使用IIS 7.0 / 7.5 时配置HttpModules需要注意

今天将站点部署到Win7 64bit的IIS下,发现网站下所有自定义的HttpModules不管用了?!

自己看了看IIS7.5的各项配置,发现iis751 iis752 都没有我的HttpModules。

仔细一想,才恍然大


在 Visual Studio 中,测试 IHttpModule(httpModules) 正常,但是放到服务器上去就不起作用了,这多半得多服务器 IIS 配置入手。

一、看“应用程序池”的“托管管道模型”是否正确

要使用“集成”才正确,网上有说得用“经典”模式,但我测试得用“集成”模式。

二、web.config 配置是否适合 IIS 版本

在 IIS 7 以下的版本中,应用以下配置:

在 IIS 7 以下的版本中,应用以下配置:

.web>
  >
     name="Cftea.MyHttpModule" type="CfteaHttpModule程序集" />
  >
.web>

在 IIS 7 及以上的版本中,应用以下配置:

.webServer>
  >
     name="Cftea.MyHttpModule" type="CfteaHttpModule程序集" />
  >
.webServer>

使用IIS 7.0 / 7.5 时配置HttpModules需要注意

今天将站点部署到Win7 64bit的IIS下,发现网站下所有自定义的HttpModules不管用了?!

自己看了看IIS7.5的各项配置,发现iis751 iis752 都没有我的HttpModules。

仔细一想,才恍然大悟!

原来我的网站应用程序池使用了默认的“集成模式”

iis753

所以所有的http请求处理都托管给了IIS,我自己的配置当然就不起效了。

将程序池的模式改为“经典”之后,一切正常。

另外,IIS7.x在承载传统的ASP.NET程序时,还有不少需要注意的地方,以后有时间慢慢分享,希望能够帮助又需要的同学们。

0 0
原创粉丝点击