【问题标题】:How can I load a webpage into an iframe, using ASP.NET MVC?如何使用 ASP.NET MVC 将网页加载到 iframe 中?
【发布时间】:2014-12-09 19:36:59
【问题描述】:

我有一个需要使用 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 中?我一直在尝试适应这一点,但还没有成功。

【问题讨论】:

  • 为什么不使用 JavaScript 在浏览器中进行呢?在这种情况下,您甚至不需要使用 iframe。
  • @Floremin:最终,我做了类似的事情,尽管 Vsevolod Goloviznin 回答了我所问的问题。在 c# 中将页面加载到 iframe 中的一些问题(即跨域脚本)意味着我改为使用 JavaScript 将网页直接加载到 iframe 中。

标签: c# asp.net asp.net-mvc iframe


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多