Using Classic ASP, PHP and WebForm Pages in ASP.NET MVC Views
Introduction:
The ASP.NET MVC framework is becoming more and more popular every day. With ASP.NET MVC 3(or upcoming ASP.NET MVC 4), you can build rich modern web applications. If you are developing an application in ASP.NET MVC then sometimes you may need to include a Classic ASP, PHP or WebForm page inside your ASP.NET MVC view for various reasons. For example, you may need to reuse the functionalities of an existing page, you may need to quickly convert a page into ASP.NET MVC, you may need to use a feature which is not directly available in ASP.NET MVC, you may need interoperability between ASP.NET MVC and other frameworks/languages, etc. In this walk through, I will show you the possible options for including Classic ASP, PHP or WebForm page inside a ASP.NET MVC view.
Description:
This article assumes that you have already setup the IIS for running Classic ASP and PHP pages. For demonstration purpose, let's create a sample ASP.NET MVC 3 application. Next, add a HomeController with a default Index action method and a Index view in this application. Next, add a Classic ASP page(MyClassicASPPage.asp) and add the following lines inside this page,
<% Dim message message = "This is a Classic ASP Page." Response.Write (message) %>
Next, add a PHP page(MyPHPPage.php) and add the following lines inside this page,
<?php $message ="This is a PHP Page."; echo $message; ?>
Next, add a WebForm page(MyWebFormPage.aspx) and add the following lines inside this page,
<% var message = "This is a WebForm Page."; Response.Write(message); %>
Now, we have a Classic ASP page, a PHP page and a WebForm page. We need to include these pages in a ASP.NET MVC view. There are at least 3 ways to include these pages in a ASP.NET MVC view.
First way: include these pages via iframe,
<!DOCTYPE html> <html> <head> <title>Home Page</title> </head> <body> <div> <div id="classicAspContainer"> <iframe src="@Url.Content(" mce_src="@Url.Content("~/MyClassicASPPage.asp")"></iframe> </div> <div id="phpContainer"> <iframe src="@Url.Content(" mce_src="@Url.Content("~/MyPHPPage.php")"></iframe> </div> <div id="webFormContainer"> <iframe src="@Url.Content(" mce_src="@Url.Content("~/MyWebFormPage.aspx")"></iframe> </div> </div> </body> </html>
Second way: include these pages using Ajax,
<!DOCTYPE html> <html> <head> <title>Home Page</title> </head> <body> <div> <div id="classicAspContainer"> </div> <div id="phpContainer"> </div> <div id="webFormContainer"> </div> </div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> (function () { $.get('@Url.Content("~/MyClassicASPPage.asp")', function (data) { $('#classicAspContainer').html(data); }); $.get('@Url.Content("~/MyPHPPage.php")', function (data) { $('#phpContainer').html(data); }); $.get('@Url.Content("~/MyWebFormPage.aspx")', function (data) { $('#webFormContainer').html(data); }); } ()) </script> </body> </html>
Third way: include these pages by creating and using a custom HTML helper,
public static class MyHelpers { public static HtmlString PartialResult(this HtmlHelper helper, string path) { var requestContext = helper.ViewContext.RequestContext; UrlHelper url = new UrlHelper(requestContext); var client = new WebClient(); var str = client.DownloadString(new Uri(string.Format("http://{0}{1}", requestContext.HttpContext.Request.Url.Host, url.Content(path)))); return MvcHtmlString.Create(str); } }
@using Helpers <!DOCTYPE html> <html> <head> <title>Home Page</title> </head> <body> <div> <div id="classicAspContainer"> @Html.PartialResult("~/MyClassicASPPage.asp") </div> <div id="phpContainer"> @Html.PartialResult("~/MyPHPPage.php") </div> <div id="webFormContainer"> @Html.PartialResult("~/MyWebFormPage.aspx") </div> </div> </body> </html>
Out of these options, you can select the one which fits your needs. Every option has its own pros and cons. With iframe, the browser will treat the pages as a brand new pages with their own DOM. But lot of people avoid using iframe for various reasons. In iframe and Ajax case, the browser will send 3 separate requests to the server. In HTML helper and Ajax case, your complete page will share the same DOM. Browser will not send separate requests in HTML helper case. So, choose the option that makes you feel comfortable.
Summary:
Sometimes during ASP.NET MVC development you may need to include Classic ASP, PHP or WebForm page inside ASP.NET MVC view for various reasons. In this article, I showed you some options to include Classic ASP, PHP or WebForm page in ASP.NET MVC view. You can download the complete code of this blog post. Hopefully you will enjoy this article too.