从http响应标头中删除服务器标签

时间:2012-04-03 14:17:32

标签: asp.net namespaces http-headers class-library

我正在尝试从asp.net中的http响应头中删除服务器标记(C#) 在堆栈溢出的帮助下,我成功创建了.dll文件并在我的项目中添加了它的引用,但仍然可以在http头中看到服务器标记。下面是我的.dll文件的代码。

using System;
using System.Web;
namespace ServerHeaders
{
public class CustomServerHeaderModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreSendRequestHeaders += OnPreSendRequestHeaders;
    }
    public void Dispose() { }
    static void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
        // remove the "Server" Http Header
        HttpContext.Current.Response.Headers.Remove("Server");

    }
}
}

我在网站上添加了对此文件的引用,并在web.config中添加了必需的标记,如下所示。

<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0"/>
<httpRuntime enableVersionHeader="false" />
<httpModules>
  <add name="CustomServerHeaderModule1" type="ServerHeaders.CustomServerHeaderModule" />
</httpModules>
</system.web>

<system.serviceModel>
    <bindings/>
    <client/>
</system.serviceModel>
</configuration>

请现在指导我还有什么遗失或有任何错误吗? 我修正了web.config中的错误。现在它在

模块中显示错误
HttpContext.Current.Response.Headers.Remove("Server");

错误是对象引用未设置为对象的实例。如何解决这个错误?我从本地主机运行项目(使用 F5 )。

0 个答案:

没有答案
相关问题