Asp.net mvc 4 如何使用web.config设置自定义标题?

Asp.net mvc 4 如何使用web.config设置自定义标题?,asp.net-mvc-4,visual-studio-2012,iis-7.5,windows-server-2008-r2,Asp.net Mvc 4,Visual Studio 2012,Iis 7.5,Windows Server 2008 R2,我在web.config中有以下内容,但在服务器上发布到IIS 7.5后,在IIS->HTTP响应头下找不到它们 我发现服务器上的web.config也没有这些条目,但它们在发布之前就存在了。因此,我只能说发布过程将它们剥离出来,但是web.configtransform文件中没有删除它们的内容。那么,为什么它们从发布的“web.config”中消失了呢 <system.webServer> <httpProtocol> <customHead

我在web.config中有以下内容,但在服务器上发布到IIS 7.5后,在
IIS->HTTP响应头下找不到它们

我发现服务器上的
web.config
也没有这些条目,但它们在发布之前就存在了。因此,我只能说发布过程将它们剥离出来,但是
web.config
transform文件中没有删除它们的内容。那么,为什么它们从发布的“web.config”中消失了呢

 <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

您确定web.config是进行此操作的最佳位置吗?我倾向于选择自定义ActionFilter。这为您提供了选择逻辑发生的时间(以何种方式)的机会,还提供了更多的控制(特别是异常处理,在操作生命周期的各个阶段应该做什么)

Microsoft将此方法用于在操作执行之前发生的调用

一些示例代码
您有不同的调试和发布配置吗?同意Jason的意见,检查您的web.config转换。
    public class CustomFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //add in your custom headers
            filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
            filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Methods", "GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS");

            base.OnActionExecuting(filterContext);
        }

        public void OnException(ExceptionContext filterContext)
        {
          //do some cool exception handling here
        }
    }