【问题标题】:.NET IIS7 can't remove X-AspNetMvc-Version header?.NET IIS7 无法删除 X-AspNetMvc-Version 标头?
【发布时间】:2017-12-22 01:58:01
【问题描述】:

我已尝试以下所有方法来删除“X-AspNetMvc-Version”标头,但它仍然出现? (X-AspNetMvc-版本:5.2)

IIS 没有添加任何标头。是否有其他与标​​题冲突的东西导致它仍然显示?

非常感谢任何帮助。提前致谢。

web.config

<httpRuntime requestValidationMode="2.0" enableVersionHeader="false" targetFramework="4.5" maxRequestLength="1048576" />

Global.asax.cs -(尝试 1)

MvcHandler.DisableMvcResponseHeader = true; 

Global.asax.cs -(尝试 2)

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version");
}

Global.asax.cs -(尝试 3)

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var application = sender as HttpApplication;
    if (application != null && application.Context != null)
    {
        application.Context.Response.Headers.Remove("X-AspNetMvc-Version");
    }
}

这是完整的 Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

namespace website
{

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            MvcHandler.DisableMvcResponseHeader = true; 
        }
    }
}

【问题讨论】:

  • 什么是method1?你试过把 application_start 放进去吗?
  • method1 只是我的内部参考,它没有出现在代码中。
  • 请附上您完整的Global.asax.cs 源代码。此问题是在本地发生还是在单独的服务器上发生?
  • 我已将完整的 Global.asax.cs 代码添加到原始问题中。
  • 大家有什么想法吗?

标签: c# asp.net-mvc iis-7


【解决方案1】:

我在发布网站时遇到了同样的问题。

解决我的问题的唯一方法是在Global.asax.cs 中添加此代码:

protected void Application_BeginRequest(object sender, EventArgs e)
{
      string[] headers = { "Server", "X-AspNetMvc-Version" };
      if (!Response.HeadersWritten)
      {
        Response.AddOnSendingHeaders((c) =>
            {
                if (c != null && c.Response != null && c.Response.Headers != null)
                {
                    foreach (string header in headers)
                    {
                        if (c.Response.Headers[header] != null)
                        {
                            c.Response.Headers.Remove(header);
                        }
                    }
                }
            });
        }

}

注意:这会删除 X-AspNetMvc-VersionServer 标头。

【讨论】:

  • 不确定这是做什么的,但它确实有效。在处理这个丑陋的废话数小时后,它救了我。 +1
猜你喜欢
  • 2019-10-26
  • 2010-11-13
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 2017-07-21
  • 1970-01-01
相关资源
最近更新 更多