Sami-L
Sami-L

Reputation: 5705

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

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:

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

    </iframe>

my Views/web.config is set as next:

and the WebForm inheritance is as next:

<%@ 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."

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

RouteTable.Routes.RouteExistingFiles = true;

but failed also,

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

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

Upvotes: 8

Views: 68169

Answers (3)

JaneH
JaneH

Reputation: 379

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

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

Upvotes: 0

jherrera
jherrera

Reputation: 181

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).

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.

UPDATE Here 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:

@{
    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:

<%@ 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>

Upvotes: 15

scartag
scartag

Reputation: 17680

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

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

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

Upvotes: 2

Related Questions