2017-04-10 52 views
0

嗨我正在開發網站使用asp .net mvc5,但是當我使用IFrame它沒有顯示任何結果。谷歌搜索和閱讀一些文件後,我發現MVC5默認停止Iframe。我在stackoverflow和其他博客上找到了一些答案,但沒有爲我工作。請檢查在那裏我做的錯誤:Iframe沒有顯示網站的Asp .Net MVC5

這裏是我的代碼:

public ActionResult Index() 
    { 
     return View(); 
    } 

筆者認爲:

<iframe width="482" height="500" scrolling="no" frameborder="0" id="paymentFrame" src="http://www.codingfusion.com"></iframe> 

我已經試過以下至今的解決方案:

After update to MVC 5, iframe no longer works

https://www.iambacon.co.uk/blog/mvc5-prevents-your-website-being-loaded-in-an-iframe

http://joost.haneveer.nl/asp-net/display-mvc-5-web-site-in-an-iframe/

這裏是我的Global.asax文件:

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 

     // MVC5 generates the "X-Frame-Options SAMEORIGIN" header by default, the following line disables the default behaviour 
     System.Web.Helpers.AntiForgeryConfig.SuppressXFrameOptionsHeader = true; 
    } 

    protected void Application_BeginRequest() 
    { 
     Response.AddHeader("X-Frame-Options", "DENY"); 
    } 

回答

0

所以最後我已經解決了我的問題,我從一開始就跟着走錯了路,有在顯示使用iframe和MVC5網站沒有問題(我不知道我是如何走上這條路的,我會隨機使用Google搜索解決方案)。真正的問題是:

我的域由SSL(https://)組成,我試圖顯示非SSL(http://)網站。這導致交叉起源和我的iframe工作不正常(我怎麼能通過查看我的頁面的源代碼查看網站的完整代碼)

解決方案:幸運的是,我試圖在我的iframe中顯示的網站支持SSL (https://)所以我只添加了(https://)到我的Iframe源代碼。

0

你明確地添加X-Frame-Options: DENY頭。將其更改爲ALLOW-FROM http://foo.com,其中「foo.com」是設置此標題的站點的站點的URL。那麼,你應該沒問題。或者,你可以在這裏不添加標題,因爲你已經在壓制它了。

+0

謝謝,但它不工作,我也試着刪除的Application_BeginRequest() –

1

只是刪除下面的代碼:

protected void Application_BeginRequest() 
    { 
     Response.AddHeader("X-Frame-Options", "DENY"); 
    } 
+0

試過,但沒有工作 –

+0

幀會被許多選項 像(應用層,網絡層,防火牆模塊,也可以通過被阻擋的ISP)。 您只需使用您的應用程序層 –