2013-03-16 75 views
5

我有一個名爲ProductsController的控制器,我使用索引方法加載了一個名爲WebForm1.aspx的Web窗體的索引視圖,索引視圖已經設置好,工作正常。現在我想在索引視圖中添加一個iframe以顯示WebForm1.aspx的內容。這兩個視圖都在MVC項目的相同文件夾視圖/產品中。我做了以下內容:iframe在MVC視圖加載asp.net web窗體

<iframe src="/ProductsController/Index?WebForm1.aspx" width="1000" height="400"> 

    </iframe> 

我的意見/ web.config中設置爲下一個:

和WebForm的繼承是未來:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> 

然而, iframe會顯示一條錯誤消息:「HTTP 404 - 您正在查找的資源(或其某個依賴項)可能已被刪除編輯,名稱已更改,或者暫時不可用。「

我也嘗試添加到我的Global.asax文件的下一行:

RouteTable.Routes.RouteExistingFiles = true; 

也失敗了,

的獨特的方式,我得到的iFrame顯示而空是使用全物理路徑如下:

<iframe src="C\:Folder1\Folder2\ ...\Views\Products\WebForm1.aspx" width="1000" height="400"> 

    </iframe> 

有人可以解釋爲什麼它不起作用嗎?以及如何使其工作?謝謝。

回答

1

您可以指向Webform.aspx文件的實際位置。

<iframe src="/FolderPath/WebForm1.aspx" width="1000" height="400"> 

    </iframe> 

你的代碼asssumes的MVC運行時將在一個名爲「ProductsController的」文件夾,名爲「索引」

子文件夾中找如果WebForm1.aspx文件確實是在該目錄結構,轉變src屬性爲src="/ProductsController/Index/WebForm1.aspx"

13

您應該將.aspx文件(webform)從視圖文件夾中取出,因爲通常來自瀏覽器的任何調用都被「BlockViewHandler」阻止(您可以在web.config文件中看到該文件視圖文件夾)。

在您創建的任何其他文件夾中,它應該在沒有控制器的情況下工作。 例如,如果您將其放在「/webforms/webform1.aspx」中,則該路徑是要使用iframe的路徑。

UPDATE這裏是在問題的新的信息的例子,希望它能幫助:

控制器:

public class ProductsController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); //Razor file in Views/Products/Index.cshtml 
    } 

    public ActionResult ActionThatRetrieveAndAspx() 
    { 
     return View("WebForm1"); //Aspx file Views/Products/WebForm1.aspx 
    } 
} 

產品的Index.html的內容,調用aspx文件通過iframe:

@{ 
    ViewBag.Title = "Index title"; 
} 

<h2>Index</h2> 

Calling aspx file from iframe: 

<iframe src="@Url.Action("ActionThatRetrieveAndAspx","Products")" width="1000" height="400"></iframe> 

產品WebForm1的內容。ASPX:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
     <title>Title</title> 
    </head> 
    <body style="background-color: #999999; padding: 10px;"> 
     This is ActionThatRetrieveAndAspx WebForm1.aspx 
    </body> 
</html> 
+0

「/webforms/webform1.aspx」開放使導航鏈接導航返回到你的MVC網站,你的意思該webforms直接在項目的根路徑?如果是的話,它不起作用。 – 2013-03-16 17:16:31

+1

這是第二個答案...我的意思是,如果您有一個從System.Web.UI.Page(WebForm1.aspx)繼承的aspx文件,那種文件應放置在Views文件夾之外,那樣你可以通過「普通」url調用它,而不通過控制器傳遞請求......另一種aspx是從「System.Web.Mvc.ViewPage」繼承的那些文件,這些文件的行爲與Razor文件相同,應放置在視圖文件夾和請求是通過一個控制器。 – jherrera 2013-03-16 17:33:54

+0

感謝您的解釋,更新了我的問題,請看看。 – 2013-03-16 18:15:05

0

我把網絡表單到MVC項目的根文件夾,並調用它像這樣:

<iframe src="~/webform.aspx"...></iframe> 

我不引用它的控制器。

一個不錯的獎金,以這種技術,你可以很容易地在網絡表單中的目標=「_父」

相關問題