简体   繁体   English

如何使用ASP.NET MVC将网页加载到iframe中?

[英]How can I load a webpage into an iframe, using ASP.NET MVC?

I have an external webpage that I need to access using a HTTP Post, and include in an iframe. 我有一个需要使用HTTP Post访问的外部网页,并且包含在iframe中。 The webpage has an example set of instructions to access the page, listed below, but they assume a Windows Form solution; 该网页具有访问页面的示例示例,如下所示,但是它们假定使用Windows窗体解决方案。 I am using ASP.NET MVC, instead. 我正在使用ASP.NET MVC。

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? 如何将这种以WebBrowser为中心的解决方案转换为可以成功发布到外部网站的内容,并将结果加载到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);
}

Is there a way to do something like this, with a HttpWebResponse (or some other control), and include this in an iframe? 有没有办法使用HttpWebResponse(或其他控件)来执行类似的操作,并将其包含在iframe中? I've been trying to adapt this, but without success yet. 我一直在尝试适应这种情况,但是还没有成功。

According to w3schools : 根据w3schools

An inline frame is used to embed another document within the current HTML document.

That said, iframe is used to load page contents via a given url and present it to user for interaction. 也就是说, iframe用于通过给定的URL加载页面内容,并将其呈现给用户进行交互。 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. 您无法将HTTP请求发送到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: 对于使用iframe的纯服务器端解决方案,您应该执行以下操作:

1.Create an action method, that will do the HTTP POST request to the url you need and fetch the result for presentation: 1.创建一个操作方法,该方法将对您所需的URL进行HTTP POST请求,并获取结果以进行演示:

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

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. 2.在您的网页中,您将创建一个iframe ,该iframe指向您的操作PostAndShow ,并将向第三方服务器显示HTTP POST请求的结果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM