Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've implemented an asp.net MVC site. I want to prevent the whole site to load in the iframe except one page. I added below code under web config.
<httpProtocol>
      <customHeaders>
        <add name="X-Frame-Options" value="SAMEORIGIN" />
      </customHeaders>
    </httpProtocol>


What I have tried:

This code of web config prevents the whole site to load in the iframe. I try to remove 'X-Frame-Options' option from the view where only I want to allow the iframe
public ActionResult Ads()
       {
           Response.Headers.Remove("X-Frame-Options");
           return View();
       }

But when i run this code X-Frame-Options not removed from header. Please suggest me what can I do?
Posted
Updated 11-Sep-18 7:08am

1 solution

Use a <location> tag in the config file to modify the headers for that action:
XML
<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="X-Frame-Options" value="SAMEORIGIN" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

<location path="YourController/Ads">
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <remove name="X-Frame-Options" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</location>
 
Share this answer
 
v2
Comments
Deepak Tiwari (D'pak) 14-Sep-18 8:30am    
Thanks Richard. It is working fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900