iframe没有在Asp .Net MVC5中显示网站

时间:2017-04-10 12:44:47

标签: iframe asp.net-mvc-5

您好我正在使用asp .net mvc5开发网站,但是当我使用IFrame时,它没有显示任何结果。在谷歌搜索和阅读一些文件后,我发现MVC5默认停止Iframe。我在stackoverflow和其他博客上找到了一些答案,但对我来说没有任何作用。请检查我在哪里做错了:

这是我的代码:

 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");
    }

3 个答案:

答案 0 :(得分:1)

只需删除以下代码:

Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
    Dim graph As Graphics = Nothing
    Dim bounds As Rectangle = Nothing
    Dim screenshot As System.Drawing.Bitmap

    Dim location As Drawing.Point = PanelPicture.PointToScreen(Drawing.Point.Empty)
    screenshot = New System.Drawing.Bitmap(PanelPicture.Width, PanelPicture.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    graph = Graphics.FromImage(screenshot)
    graph.CopyFromScreen(location.X, location.Y, 0, 0, PanelPicture.Size, CopyPixelOperation.SourceCopy)
    picFinal.Image = screenshot

    graph.Dispose()
End Sub

答案 1 :(得分:0)

您明确添加了X-Frame-Options: DENY标头。将其更改为ALLOW-FROM http://foo.com,其中“foo.com”是您构建设置此标头的网站的网站的网址。那你应该没问题。或者,你可以在这里根本不添加标题,因为你已经压制它了。

答案 2 :(得分:0)

所以最后我已经解决了我的问题,我从一开始就遵循了错误的路径,使用Iframe和MVC5显示网站没有问题(我不知道我是如何走这条路,我是google搜索并随机尝试解决方案) 。真正的问题是:

My Domain由SSL(https://)组成,我试图显示非SSL(http://)网站。这导致交叉起源和我的Iframe无法正常工作(我怎么能通过查看我的页面的源代码来查看网站的完整代码)

解决方案:幸运的是,我尝试在我的Iframe中显示的网站支持SSL(https://)所以我只添加了(https://)到我的Iframe源。