【问题标题】:My custom httpModule is not being detected with MVCMVC 未检测到我的自定义 httpModule
【发布时间】:2013-06-28 11:44:20
【问题描述】:

我创建了一个自定义模块:

namespace KittenFarm.ServerModules
{
    public class CustomServerHeaderModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += OnPreSendRequestHeaders;
        }

        public void Dispose()
        { }

        void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Headers.Remove("Server");
        }
    } 
}

我已经在我的网络配置中注册了它:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
        <add name="CustomServerHeader" type="CustomServerHeaderModule" />
    </modules>
    ....

但是,它似乎永远不会运行。

我怀疑这是一个命名空间问题,但我已经尝试了 type= 部分中我能想到的所有命名空间组合,但它从未达到我放入的断点。

有什么想法吗?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 httpmodule


    【解决方案1】:

    你是对的,类型中缺少的是你的命名空间。

    以下内容对我有用:

       <modules runAllManagedModulesForAllRequests="true">
          <add name="CustomServerHeader" type="KittenFarm.ServerModules.CustomServerHeaderModule" />
       </modules>
    

    【讨论】:

      【解决方案2】:

      问题在于解决方案设置为使用 Visual Studio 开发服务器,它不接收 http 模块。在我将其更改为使用本地 IIS 后,它就可以工作了。

      【讨论】:

        【解决方案3】:

        您可以将函数设置为在调用 Application_Start 方法之前运行。在 global.asax 中添加以下代码

        [程序集:PreApplicationStartMethod(typeof(CS.MVCHttpModule.MvcApplication),"Register")]

        http://dotnetlionet.blogspot.in/2015_06_01_archive.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-08
          • 1970-01-01
          • 1970-01-01
          • 2020-05-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多