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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15450983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:32:46  来源:igfitidea点击:

iframe in an MVC view to load asp.net web form

asp.nethtmliframeasp.net-mvc-4

提问by Sami-L

I have a controller called ProductsController and I use the index method to load the index view with a web form inside called WebForm1.aspx, The index view has been set up and is working properly. Now I want to add an iframe in the index view to display the content of WebForm1.aspx. Both views are in the same folder Views/Products in the MVC project. I did the following:

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

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

    </iframe>

my Views/web.config is set as next:

我的 Views/web.config 设置如下:

and the WebForm inheritance is as next:

并且 WebForm 继承如下:

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

Yet the iframe display an error message: "HTTP 404 - The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable."

但是 iframe 显示一条错误消息:“HTTP 404 - 您正在查找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用。”

I tried also to add to my global.asax file the next line:

我还尝试将下一行添加到我的 global.asax 文件中:

RouteTable.Routes.RouteExistingFiles = true;

but failed also,

但也失败了,

The unique way I get iFrame displayed but emptyis to use the full physical path as next:

我让 iFrame 显示但为空的独特方式是使用完整的物理路径作为下一个:

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

    </iframe>

can someone explain why it does not work? And how to make it work? Thank you.

有人可以解释为什么它不起作用吗?以及如何让它发挥作用?谢谢你。

回答by jherrera

You should put your .aspx file (webform) out of the views folder, because normally any call from the browser is block by the "BlockViewHandler" (which you can see in the web.config file of the views folder).

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

In any other folder that you create, it should work without a controller. For example if you put it in "/webforms/webform1.aspx" that path is the one to use un the iframe.

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

UPDATEHere is an example with the new information in the question, hope it can help:

更新这是问题中新信息的示例,希望它可以帮助:

The controller:

控制器:

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
    }
}

The content of Products Index.html, calling the aspx file via an iframe:

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>

The content of Products WebForm1.aspx:

Products 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>

回答by scartag

You can point to the actual location of the Webform.aspx file.

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

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

    </iframe>

Your code asssumes that the MVC runtime would be looking at a folder called "ProductsController" and a sub folder called "Index"

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

If you Webform1.aspx file is really in that directory structure, change the src attribute to src="/ProductsController/Index/WebForm1.aspx"

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

回答by JaneH

I put the webform into the root folder of the MVC project and call it like this:

我把webform放到MVC项目的根文件夹中,这样调用:

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

I do not reference it in the controller.

我没有在控制器中引用它。

A nice bonus to this technique is that you can easily navigate back to your MVC website by making navigation links in the webform open in target="_parent"

这种技术的一个很好的好处是,您可以通过在目标 =“_parent”中打开 webform 中的导航链接轻松导航回您的 MVC 网站