ASP.NET MVC 項目設置,移除多余的響應頭,woff,woff2 字體文件請求處理


移除 X-AspNetMvc-Version

在 Global.asax 的 Application_Start添加代碼 

MvcHandler.DisableMvcResponseHeader = true; 

移除 X-AspNet-Version

在 web.config 下的 system.web 節點下設置 enableVersionHeader=false

<httpRuntime targetFramework="4.5" enableVersionHeader="false" />

移除 X-Powered-By

在 web.config 下的 system.webServer 添加 一下代碼

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

 

移除 Server

有兩種方式,以下方式任選其中一個。

1. 在 global.asax 重寫 Application_PreSendRequestHeaders 方法

protected void Applcation_PreSendRequestHeaders(object sender, EventArgs e)
        {
            var application = sender as HttpApplication;
            if (application != null && application.Context != null)
            {
                application.Context.Response.Headers.Remove("Server");
                //application.Context.Response.Headers.Set("Server", "MY Server");
            }
        }

2. 新建一個 HttpMoudle 來處理

public class SiteHeaderMoudle : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += context_PreSendRequestHeaders;
        }

        private void context_PreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Headers.Remove("Server");
        }
    }

然后再 webconfig 文件中注冊

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="SiteHeaderMoudle" type="MY.web.Extended.SiteHeaderMoudle"/>  // 注意類的命名空間
    </modules>
    <staticContent>
      <!--修復瀏覽器訪問bootstrap以下后綴的字體notfound錯誤-->
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
    </staticContent>
  </system.webServer>

其他設置,只保留 Razor 視圖引擎

protected void Application_Start()
        {
            // Removing all the view engines
            ViewEngines.Engines.Clear();
            //Add Razor Engine (which we are using)
            ViewEngines.Engines.Add(new RazorViewEngine());
        }

 

參考文章

http://www.cnblogs.com/CareySon/archive/2009/12/14/1623624.html

https://blogs.technet.microsoft.com/stefan_gossner/2008/03/12/iis-7-how-to-send-a-custom-server-http-header/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM