zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    你看懂了程序员的这些笑话吗?
    SharePoint 2013 Designer 自己定义操作菜单
    Ubuntu下用glade和GTK+开发C语言界面程序(三)——学习make的使用方法
    【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)
    LeetCode 122 Best Time to Buy and Sell Stock II
    Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性
    HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)
    TS4
    angularjs1-过滤器
    angularjs1- ng-include
  • 原文地址:https://www.cnblogs.com/wubh/p/9507167.html
Copyright © 2011-2022 走看看