繁体   English   中英

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

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

我有一个需要使用HTTP Post访问的外部网页,并且包含在iframe中。 该网页具有访问页面的示例示例,如下所示,但是它们假定使用Windows窗体解决方案。 我正在使用ASP.NET MVC。

如何将这种以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);
}

有没有办法使用HttpWebResponse(或其他控件)来执行类似的操作,并将其包含在iframe中? 我一直在尝试适应这种情况,但是还没有成功。

根据w3schools

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

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

要考虑的另一件事是,可以保护您正在加载的网页不会嵌入到iframe

实际上,有一些选择可以实现您想要的目标。 对于使用iframe的纯服务器端解决方案,您应该执行以下操作:

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.在您的网页中,您将创建一个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