簡體   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