MVC没有检测到我的自定义httpModule



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

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=部分中我能想到的所有名称空间组合,但它从未达到我在其中设置的断点。

有什么想法吗?

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

以下对我有效:

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

问题是解决方案被设置为使用Visual Studio Development Server,它不接收http模块。在我将其更改为使用本地IIS后,它就工作了。

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

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

请参阅http://dotnetlionet.blogspot.in/2015_06_01_archive.html

最新更新