【问题标题】:httpModules not working on iis7httpModules 在 iis7 上不起作用
【发布时间】:2016-01-01 04:10:32
【问题描述】:

我有以下模块

public class LowerCaseRequest : IHttpModule {
     public void Init(HttpApplication context) {
         context.BeginRequest += new EventHandler(this.OnBeginRequest);
     }

     public void Dispose() { }

     public void OnBeginRequest(Object s, EventArgs e) {
         HttpApplication app = (HttpApplication)s;

         if (app.Context.Request.Url.ToString().ToLower().EndsWith(".aspx")) {
             if (app.Context.Request.Url.ToString() != app.Context.Request.Url.ToString().ToLower()) {
                 HttpResponse response = app.Context.Response;

                 response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                 response.Status = "301 Moved Permanently";
                 response.RedirectLocation = app.Context.Request.Url.ToString().ToLower();
                 response.SuppressContent = true;
                 response.End();
             }
             if (!app.Context.Request.Url.ToString().StartsWith(@"http://zeeprico.com")) {
                 HttpResponse response = app.Context.Response;

                 response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                 response.Status = "301 Moved Permanently";
                 response.RedirectLocation = app.Context.Request.Url.ToString().ToLower().Replace(@"http://zeeprico.com", @"http://www.zeeprico.com");
                 response.SuppressContent = true;
                 response.End();
             }
         }
     }
}

web.config 看起来像

<system.web>
  <httpModules>
      <remove name="WindowsAuthentication" />
      <remove name="PassportAuthentication" />
      <remove name="AnonymousIdentification" />
      <remove name="UrlAuthorization" />
      <remove name="FileAuthorization" />
      <add name="LowerCaseRequest" type="LowerCaseRequest" />
      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>
</system.web>

它可以在我运行 XP 和 IIS 5.1 的 PC 上运行

但是在我的运行 IIS7 和 WS 2008 的网络服务器上不起作用,请帮助我不知道如何解决这个问题。

谢谢

【问题讨论】:

    标签: c# asp.net asp.net-2.0 httphandler httpmodule


    【解决方案1】:

    在 IIS7 及更高版本上使用

    <configuration>
      <system.webServer>
        <modules>
          <add name="CustomModule" type="Samples.CustomModule" />
        </modules>
      </system.webServer>
    </configuration>
    

    【讨论】:

    • 谢谢,因为我的开发环境是 iis5.0,而我的实际环境是 iis7,所以我实际上必须双向兼顾
    【解决方案2】:

    以上对于 IIS 7.5 是正确的

    <modules>
      <add name="CustomModule" type="Samples.CustomModule" />
    </modules>
    

    我遇到的唯一问题是,特定应用程序的应用程序池实例应设置为托管管道 = 集成,而不是经典......
    或者: 使用经典模式

    如果您的应用程序要使用经典模式,请确保您的应用程序已针对该类型的池进行配置,并且您的模块已在 system.web 部分配置,而不是在 web.config 文件的 system.webServer 部分配置。

    【讨论】:

    • 这是一个更完整的答案。除了应用程序在经典模式下运行之外,我的 web.config 值与接受的答案相同。
    【解决方案3】:

    在 IIS 中转到功能视图选择模块

    双击模块,然后右键单击并按(添加托管模块)

    然后输入web.config中定义的名称和类型

    示例:

    <httpModules>
      <add name="CustomModule" type="WebApplication.Security.CustomModule" />
    </httpModules>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 2016-06-22
      • 1970-01-01
      相关资源
      最近更新 更多