find and solve || findandsolve.com
Please wait.....

How to Remove X-Frame option in asp.net core


OnResultExecuting happens too early in the MVC lifecycle. The header has not been set yet.
What you need is the OnResultExecuted method which is run after the View is rendered.
Here's how you write a filter class for what you are looking for:


using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DTE.Utilities
{
        public class RemoveXFrameOptionsAttribute : ActionFilterAttribute
        {
            public override void OnResultExecuted(ResultExecutedContext filterContext)
            {
                filterContext.HttpContext.Response.Headers.Remove("X-Frame-Options");
                base.OnResultExecuted(filterContext);
            }
    }
}

Then you can use it, decorate whatever Controller or Action you want this filter applied.
[RemoveXFrameOptions]
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
}
Or
public class DemoController : Controller
{
[RemoveXFrameOptions]
public ActionResult Index()
{
return View();
}
}

For Hole Controller you can use this code for your Startup.cs
           app.Use(async (context, next) =>
            {
context.Response.Headers.Remove("X-Frame-Options");
await next();
});

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response