Asp.net web api 从响应中删除标题
我需要隐藏由ASP.NET和IIS生成并在ASP.NET WebAPI服务的响应中返回的某些头。我需要隐藏的标题是:Asp.net web api 从响应中删除标题,asp.net-web-api,wcf-web-api,Asp.net Web Api,Wcf Web Api,我需要隐藏由ASP.NET和IIS生成并在ASP.NET WebAPI服务的响应中返回的某些头。我需要隐藏的标题是: 服务器 X-AspNet-Version X-AspNetMvc-Version X动力 该服务之前托管在WCF中,通过订阅PreSendRequestHeaders并操作HttpContext.Current.Response.Headers,在HttpModule中完成掩蔽。使用ASP.NET WebAPI,所有内容现在都基于任务,因此HttpContext.Curren
- 服务器
- X-AspNet-Version
- X-AspNetMvc-Version
- X动力
该服务之前托管在WCF中,通过订阅PreSendRequestHeaders并操作HttpContext.Current.Response.Headers,在HttpModule中完成掩蔽。使用ASP.NET WebAPI,所有内容现在都基于任务,因此HttpContext.Current为空。我试图插入一个消息处理程序并处理返回的HttpResponseMessage,但该阶段不存在头。可以在IIS设置中删除X-Powered-By,但是建议如何删除其余的X-Powered-By?问题是每个X-Powered-By都是在不同的点添加的:
:由IIS添加。虽然您似乎已经使用HttpModule删除了它,但并不完全确定它是否可以关闭服务器
:在刷新X-AspNet-Version
类时由System.Web.dll添加HttpResponse
:由X-AspNetMvc-Version
在System.Web.dll中添加。它可以被覆盖,所以这个应该是正常的MvcHandler
由IIS提供,但可以按您所说的关闭X-Powered-By
我认为您最好的选择仍然是使用HttpModules。如果您使用的是IIS7/Azure,请查看以下内容:
它显示了在不使用HttpModules的情况下禁用这些标题的最佳方法。为了让通过google/bing搜索登陆的用户受益: 以下是步骤摘要: 步骤1:创建一个从IHttpModule派生的类(完成后可以清理): 步骤3:现在可以按如下方式删除标题:
private void OnPreSendRequestHeaders(object sender, EventArgs e)
{
if (null == _httpApplication)
{
return;
}
if (_httpApplication.Context != null)
{
var response = _httpApplication.Response;
HeadersToCloak.ForEach(header => response.Headers.Remove(header));
}
}
步骤4:现在在system.webserver下的root web.config中注册此模块(如果运行IIS 7.0集成模式,请参阅此处的更多详细信息):
希望这有帮助 如果要删除版本,请转到web.config文件 并添加这些行
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<!--enableVersionHeader remove the header-->
<httpRuntime targetFramework="4.5.2" enableVersionHeader = "false"/>
另外,添加这些
<httpProtocol>
<customHeaders>
<!--enableVersionHeader remove the header-->
<remove name ="X-Powered-By"/>
</customHeaders>
</httpProtocol>
你是对的。我能够在HttpModule中删除它们(包括服务器)。唯一的问题是我如何获得对HttpContext的引用:HttpContext.Current返回null,但HttpApplication.Context在传递给HttpModule的HttpApplication实例上返回一个有效实例。问题是如何由自定义HttpModule删除,而不是如何删除它。我也面临着这个问题,仍然不知道如何摆脱它。使用UrlScan更耗时,而且在Windows Server 2012上不起作用。这实际上不会删除X-Powered-By
标题。至少不在IIS 7中。@Ek0nomik如果您有权访问IIS控制台,请检查HttpReportsHeaders(双击图标)并从中删除X-Powered-By标头。我也经历过,如果从IIS添加响应头,它不会被HttpModule删除-它看起来像是来自IIS的头稍后添加到管道中。
<configuration>
<system.webServer>
<modules>
<add name="MyCustomModule" type="<namespace>.MyCustomModule "/>
</modules>
</system.webServer>
</configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<!--enableVersionHeader remove the header-->
<httpRuntime targetFramework="4.5.2" enableVersionHeader = "false"/>
<httpProtocol>
<customHeaders>
<!--enableVersionHeader remove the header-->
<remove name ="X-Powered-By"/>
</customHeaders>
</httpProtocol>