我在IIS6中使用VS 2008。我想从Http Header“Server”中删除服务器标签。 我在Global.asax中使用了以下代码。
void Application_PreSendRequestHeaders(object src, EventArgs e)
{
HttpContext.Current.Response.Headers.Remove("Server");
}
它显示错误“对象引用未设置为对象的实例”。 我该如何解决这个问题
答案 0 :(得分:1)
以下是适用于我的步骤(适用于IIS6):
仅供记录...如果您升级到IIS 7(集成管道模式),您可以使用自定义HttpModule代码实现此目的。
祝你好运!
答案 1 :(得分:0)
在这里看一下答案:https://stackoverflow.com/a/12804722/2074016。它有额外的错误处理可以修复您的错误:
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
// Remove the "Server" HTTP Header from response
HttpApplication app = sender as HttpApplication;
if (null != app && null != app.Request && !app.Request.IsLocal &&
null != app.Context && null != app.Context.Response)
{
NameValueCollection headers = app.Context.Response.Headers;
if (null != headers)
{
headers.Remove("Server");
}
}
}