Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 401
Sami-L Avatar asked Mar 16 '13 15:03

Sami-L


People also ask

How do I use iframe in Webforms?

You can put an iframe in any HTML page, so you could put one inside a contentplaceholder in a webform that has a Masterpage and it will appear with whatever URL you load into it (via Javascript, or C# if you turn your iframe into a server-side control ( runat='server' ) on the final HTML page that your webform produces ...

Can we use webforms in MVC?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy.

Can you use ASPX pages in MVC?

If you add a plain ASPX page to an ASP.NET MVC project, well, it just works like a charm without any changes to the configuration. If you invoke the ASPX page, the ASPX page is processed with viewstate and postbacks.

What is the use of iframe in asp net?

The <iframe> tag specifies an inline frame. An inline frame is used to embed another document within the current HTML document.


1 Answers

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>
like image 110
jherrera Avatar answered Oct 19 '22 07:10

jherrera