在MVC视图中加载asp.net web表单的iframe

在MVC视图中加载asp.net web表单的iframe,asp.net,html,iframe,asp.net-mvc-4,Asp.net,Html,Iframe,Asp.net Mvc 4,我有一个名为ProductsController的控制器,我使用index方法加载索引视图,其中包含一个名为WebForm1.aspx的web表单,索引视图已经设置好并且工作正常。现在我想在索引视图中添加一个iframe来显示WebForm1.aspx的内容。这两个视图都位于MVC项目中相同的文件夹视图/产品中。我做了以下工作: <iframe src="/ProductsController/Index?WebForm1.aspx" width="1000" height="40

我有一个名为ProductsController的控制器,我使用index方法加载索引视图,其中包含一个名为WebForm1.aspx的web表单,索引视图已经设置好并且工作正常。现在我想在索引视图中添加一个iframe来显示WebForm1.aspx的内容。这两个视图都位于MVC项目中相同的文件夹视图/产品中。我做了以下工作:

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

    </iframe>
但也失败了,

使iFrame显示为空的唯一方法是使用完整物理路径作为下一步:

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

    </iframe>


有人能解释为什么它不起作用吗?如何让它工作?谢谢。

您可以指向Webform.aspx文件的实际位置

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

    </iframe>

您的代码假设MVC运行时将查看名为“ProductsController”的文件夹和名为“Index”的子文件夹


如果您的Webform1.aspx文件确实位于该目录结构中,请将src属性更改为
src=“/ProductsController/Index/Webform1.aspx”

您应该将您的.aspx文件(webform)放在视图文件夹之外,因为通常浏览器的任何调用都会被“BlockViewHandler”阻止(您可以在views文件夹的web.config文件中看到)

在您创建的任何其他文件夹中,它应该在没有控制器的情况下工作。 例如,如果您将其放在“/webforms/webform1.aspx”中,则该路径就是在iframe中使用的路径

更新以下是一个问题中新信息的示例,希望能有所帮助:

控制员:

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
    }
}
Products Index.html的内容,通过iframe调用aspx文件:

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

<h2>Index</h2>

Calling aspx file from iframe:

<iframe src="@Url.Action("ActionThatRetrieveAndAspx","Products")" width="1000" height="400"></iframe>
@{
ViewBag.Title=“索引标题”;
}
指数
从iframe调用aspx文件:
产品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>

标题
这是检索AndAspx WebForm1.aspx的操作

我将webform放入MVC项目的根文件夹,并将其命名为:

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

我没有在控制器中引用它


这种技术的一个很好的好处是,通过在target=“\u parent”

“/webforms/webform1.aspx”中打开webform中的导航链接,您可以轻松导航回MVC网站,您的意思是webforms直接位于项目根路径中吗?如果是,则不起作用。这是第二个答案…我的意思是,如果您有一个从System.Web.UI.Page(WebForm1.aspx)继承的aspx文件,则该类文件应放在Views文件夹之外,这样您可以通过“普通”调用它url而不通过控制器传递请求…另一种类型的aspx是从“System.Web.Mvc.ViewPage”继承的。这些文件表现为Razor文件,应该放在views文件夹中,请求通过控制器进行。感谢解释,更新了我的问题,请看一看。
<%@ 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>
<iframe src="~/webform.aspx"...></iframe>