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,
1 |
<%
|
2 |
3 |
Dim message
|
4 |
message = "This is a Classic ASP Page."
|
5 |
Response.Write (message)
|
6 |
7 |
%>
|
Next, add a PHP page(MyPHPPage.php) and add the following lines inside this page,
1 |
<?php
|
2 |
3 |
$message ="This is a PHP Page.";
|
4 |
echo $message;
|
5 |
6 |
?>
|
Next, add a WebForm page(MyWebFormPage.aspx) and add the following lines inside this page,
1 |
<%
|
2 |
3 |
var message = "This is a WebForm Page.";
|
4 |
Response.Write(message);
|
5 |
6 |
%>
|
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,
01 |
<!DOCTYPE html>
|
02 |
<html>
|
03 |
<head>
|
04 |
<title>Home Page</title>
|
05 |
</head>
|
06 |
<body>
|
07 |
<div>
|
08 |
<div
id="classicAspContainer">
|
09 |
<iframe
src="@Url.Content("
mce_src="@Url.Content("~/MyClassicASPPage.asp")"></iframe>
|
10 |
</div>
|
11 |
<div
id="phpContainer">
|
12 |
<iframe
src="@Url.Content("
mce_src="@Url.Content("~/MyPHPPage.php")"></iframe>
|
13 |
</div>
|
14 |
<div
id="webFormContainer">
|
15 |
<iframe
src="@Url.Content("
mce_src="@Url.Content("~/MyWebFormPage.aspx")"></iframe>
|
16 |
</div>
|
17 |
</div>
|
18 |
</body>
|
19 |
</html>
|
Second way: include these pages using Ajax,
01 |
<!DOCTYPE html>
|
02 |
<html>
|
03 |
<head>
|
04 |
<title>Home Page</title>
|
05 |
</head>
|
06 |
<body>
|
07 |
<div>
|
08 |
<div
id="classicAspContainer">
|
09 |
</div>
|
10 |
<div
id="phpContainer">
|
11 |
</div>
|
12 |
<div
id="webFormContainer">
|
13 |
</div>
|
14 |
</div>
|
15 |
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
|
16 |
<script>
|
17 |
(function () {
|
18 |
$.get('@Url.Content("~/MyClassicASPPage.asp")',
function (data) {
|
19 |
$('#classicAspContainer').html(data);
|
20 |
});
|
21 |
$.get('@Url.Content("~/MyPHPPage.php")',
function (data) {
|
22 |
$('#phpContainer').html(data);
|
23 |
});
|
24 |
$.get('@Url.Content("~/MyWebFormPage.aspx")',
function (data) {
|
25 |
$('#webFormContainer').html(data);
|
26 |
});
|
27 |
} ())
|
28 |
</script>
|
29 |
</body>
|
30 |
</html>
|
Third way: include these pages by creating and using a custom HTML helper,
01 |
public
static
class
MyHelpers
|
02 |
{
|
03 |
public
static
HtmlString PartialResult(this
HtmlHelper helper, string
path)
|
04 |
{
|
05 |
var requestContext =
helper.ViewContext.RequestContext;
|
06 |
UrlHelper url = new
UrlHelper(requestContext);
|
07 |
var client = new
WebClient();
|
08 |
var str = client.DownloadString(new
Uri(string.Format("http://{0}{1}",
requestContext.HttpContext.Request.Url.Host,
url.Content(path))));
|
09 |
return
MvcHtmlString.Create(str);
|
10 |
}
|
11 |
}
|
01 |
@using Helpers
|
02 |
<!DOCTYPE html>
|
03 |
<html>
|
04 |
<head>
|
05 |
<title>Home Page</title>
|
06 |
</head>
|
07 |
<body>
|
08 |
<div>
|
09 |
<div
id="classicAspContainer">
|
10 |
@Html.PartialResult("~/MyClassicASPPage.asp")
|
11 |
</div>
|
12 |
<div
id="phpContainer">
|
13 |
@Html.PartialResult("~/MyPHPPage.php")
|
14 |
</div>
|
15 |
<div
id="webFormContainer">
|
16 |
@Html.PartialResult("~/MyWebFormPage.aspx")
|
17 |
</div>
|
18 |
</div>
|
19 |
</body>
|
20 |
</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.