如何从响应中删除 X-Frame-Options

How to remove X-Frame-Options from the response

我对 X-Frame-Options http header 有疑问。

我使用 MVC 5,因此 SAMEORIGIN 选项会自动添加到 Headers 中用于 Http 响应。

我仍然想使用默认选项,我不想在 Application_Start 中使用以下行:

AntiForgeryConfig.SuppressXFrameOptionsHeader = true;

我想在控制器级别的某些特定操作中删除 X-Frame-Options header,代码如下:

base.HttpContext.Response.Headers.Remove("X-Frame-Options");

然而,它不起作用。

你知道我怎样才能删除它吗?

如有任何帮助,我们将不胜感激。

调查问题后,我注意到可以创建一个覆盖 OnResultExecuted 方法的 ActionFilter,我可以在其中删除 http header:

public class AllowIframeFromUriAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        //...
        filterContext.HttpContext.Response.Headers.Remove("X-Frame-Options");
        base.OnResultExecuted(filterContext);
    }
}

它有效,所以我想分享解决方案。