如何使用ASP.NET MVC将网页加载到iframe中? [英] How can I load a webpage into an iframe, using ASP.NET MVC?

查看:343
本文介绍了如何使用ASP.NET MVC将网页加载到iframe中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部网页,我需要使用HTTP Post访问,并包含在iframe中。该网页有一组示例访问该页面的说明,如下所示,但它们采用Windows Form解决方案;我正在使用ASP.NET MVC。

I have an external webpage that I need to access using a HTTP Post, and include in an iframe. The webpage has an example set of instructions to access the page, listed below, but they assume a Windows Form solution; I am using ASP.NET MVC, instead.

如何将这个以WebBrowser为中心的解决方案转换为可以成功发布到外部站点的内容,并将结果加载到iframe?

How can I convert this WebBrowser centric solution into something that can successfully post to the external site, and load the results into an iframe?

private WebBrowser wb_inline = null;

private void Submit_Click(object sender, EventArgs e)
{
    string url = "http://www.example.com";
    string setupParameters = "account_token=abc123";

    ServicePointManager.ServerCertificateValidationCallback = 
        (s, cert, chain, ssl) => true;
    ASCIIEncoding encoding = new ASCIIEncoding();
    var postData = setupParameters;

    if (null == wb_inline)
    {
        wb_inline = new WebBrowser(this);
        wb_inline.Location = new System.Drawing.Point(100, 277);
        wb_inline.Size = new System.Drawing.Size(675, 650);
    }
    Controls.Add(wb_inline);
    wb_inline.Visible = true;
    string AdditionalHeaders = "Content-Type: application/json";
    byte[] data = encoding.GetBytes(postData);
    wb_inline.Navigate(payProsUrl, "", data, AdditionalHeaders);
}

有没有办法做这样的事情,有一个HttpWebResponse(或者一些)其他控件),并将其包含在iframe中?我一直在尝试改编这个,但还没有成功。

Is there a way to do something like this, with a HttpWebResponse (or some other control), and include this in an iframe? I've been trying to adapt this, but without success yet.

推荐答案

根据 w3schools

使用内嵌框架在当前HTML文档中嵌入另一个文档。

那就是说, iframe 是用于通过给定的URL加载页面内容并将其呈现给用户进行交互。之后,您无法控制用户对加载页面的操作(他可以单击链接,发布表单等)。您无法将HTTP请求发送到 iframe ,因为它只是显示另一个页面的窗口,并且不支持复杂的方案。

That said, iframe is used to load page contents via a given url and present it to user for interaction. After that you have little control over what user does with the loaded page (he can click links, post forms, etc). You cannot send HTTP request to an iframe as it's just a "window" to show another page and doesn't support complicated scenarios.

还需要考虑的另一件事是,您加载的网页可以防止嵌入 iframe

One more thing to consider is that the web page you're loading can be protected from embedding into an iframe.

事实上,有一些选项可以达到你想要的效果。对于使用 iframe 的纯服务器端解决方案,您应该执行以下操作:

In fact there are some options how you can achieve what you want. For a pure server-side solution using iframe you should do the following:

1.创建一个操作方法,这将对您需要的URL执行HTTP POST请求并获取结果以进行演示:

1.Create an action method, that will do the HTTP POST request to the url you need and fetch the result for presentation:

public ActionResult PostAndShow() 
{
   //do the posting 
   string result = requestStream.ReadToEnd(); //you can have similar code to fetch the server response
   return Content(result);
}

2.在您的网页中,您将创建 iframe 将指向您的操作 PostAndShow ,并将向第三方服务器显示您的HTTP POST请求的结果。

2.In you webpage you will create an iframe that will point to your action PostAndShow and will show the result of your HTTP POST request to third-party server.

<iframe src="@Url.Action("PostAndShow", "My")" width="400" height="300"></iframe>

这篇关于如何使用ASP.NET MVC将网页加载到iframe中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆