如何删除 ASP.Net MVC 默认 HTTP 标头? [英] How to remove ASP.Net MVC Default HTTP Headers?
问题描述
我正在使用的 MVC 应用程序中的每个页面都在响应中设置这些 HTTP 标头:
Each page in an MVC application I'm working with sets these HTTP headers in responses:
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 2.0
如何防止这些显示?
推荐答案
X-Powered-By
是 IIS 中的自定义标头.从 IIS 7 开始,您可以通过将以下内容添加到 web.config
中来删除它:
X-Powered-By
is a custom header in IIS. Since IIS 7, you can remove it by adding the following to your web.config
:
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
</system.webServer>
这个标题也可以根据您的需要进行修改,更多信息请参考http://www.iis.net/ConfigReference/system.webServer/httpProtocol/customHeaders
This header can also be modified to your needs, for more information refer to http://www.iis.net/ConfigReference/system.webServer/httpProtocol/customHeaders
将此添加到 web.config
以摆脱 X-AspNet-Version
标头:
Add this to web.config
to get rid of the X-AspNet-Version
header:
<system.web>
<httpRuntime enableVersionHeader="false" />
</system.web>
<小时>
最后,要删除 X-AspNetMvc-Version
,请编辑 Global.asax.cs
并在 Application_Start
事件中添加以下内容:
Finally, to remove X-AspNetMvc-Version
, edit Global.asax.cs
and add the following in the Application_Start
event:
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
}
<小时>
您还可以在运行时通过 Global.asax.cs
中的 Application_PreSendRequestHeaders
事件修改标头.如果您的标头值是动态的,这很有用:
You can also modify headers at runtime via the Application_PreSendRequestHeaders
event in Global.asax.cs
. This is useful if your header values are dynamic:
protected void Application_PreSendRequestHeaders(object source, EventArgs e)
{
Response.Headers.Remove("foo");
Response.Headers.Add("bar", "quux");
}
这篇关于如何删除 ASP.Net MVC 默认 HTTP 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!