本文主要介绍如何优化ASP.NET MVC使用IIS时Response Header中的没必要要的信息css
默认的,建立一个ASP.NET MVC项目,会在Response Header中包含一些敏感的信息,这些信息是没有什么用处的可是会暴露出IIS的配置信息等。html
下面是默认的Response Header信息:web
Cache-Control:private, s-maxage=0
Content-Encoding:gzip
Content-Length:8024
Content-Type:text/html; charset=utf-8
Date:Fri, 30 Sep 2016 03:17:10 GMT
Server:Microsoft-IIS/10.0
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.2
X-Frame-Options:SAMEORIGIN
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpcV29ya1wyMDE2XE56TmQuSWRlbnRpdHlcR0xELldlYlxTdXBlclxVc2Vycw==?=
以上内容中,红色部分并非必须输出的信息,相反会暴露服务器的一些配置信息等,如下逐一介绍如何移除不须要的输出信息:服务器
打开Global.asax.cs ,在Application_Start方法中,添加以下代码:app
MvcHandler.DisableMvcResponseHeader = true;
一样在Global.asax.cs 中,添加以下代码优化
protected void Application_PreSendRequestHeaders(object sender, EventArgs e) { HttpApplication app = sender as HttpApplication; if (app != null && app.Context != null) { app.Context.Response.Headers.Remove("Server"); } }
在Web.config文件中找到system.web节点,添加以下配置:spa
<httpRuntime enableVersionHeader="false" />
- X-Powered-By
在Web.Config文件中找到system.webservice,添加以下配置:code
<httpProtocol> <customHeaders> <remove name="X-Powered-By" /> </customHeaders> </httpProtocol>
OK,作完上面的操做,编译后打开,F12中能够看到,Response Header内容以下:htm
Cache-Control:private, s-maxage=0
Content-Encoding:gzip
Content-Length:8018
Content-Type:text/html; charset=utf-8
Date:Fri, 30 Sep 2016 02:35:39 GMT
Vary:Accept-Encoding
X-Frame-Options:SAMEORIGIN
X-SourceFiles:=?UTF-8?B?RDpcV29ya1wyMDE2XE56TmQuSWRlbnRpdHlcR0xELldlYlxTdXBlclxVc2Vycw==?=
没必要要的信息已经被去掉了。清爽不少!ip