简体   繁体   中英

.NET IIS7 can't remove X-AspNetMvc-Version header?

I have tried all of the following ways below to remove the 'X-AspNetMvc-Version' header but it still appears? (X-AspNetMvc-Version: 5.2)

IIS doesn't have any headers added. Could there be something else that is conflicting with the header causing it to still be shown?

Any help is much appreciated. Thanks in advance.

web.config

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

Global.asax.cs - (attempt 1)

MvcHandler.DisableMvcResponseHeader = true; 

Global.asax.cs - (attempt 2)

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

Global.asax.cs - (attempt 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");
    }
}

Here is the full 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; 
        }
    }
}

I had the same problem when publishing my website.

The only thing that resolved my issue was adding this code in the 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);
                        }
                    }
                }
            });
        }

}

Note: this removes X-AspNetMvc-Version and Server headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM