Приглашаем посетить
Грин (grin.lit-info.ru)

16.5 Migrating ASP to ASP.NET

Table of Contents

Previous Next

16.5 Migrating ASP to ASP.NET

16.5.1 A brief word on compatibility of ASP and ASP.NET

As we mentioned in section 16.1, ASP.NET was developed for the Microsoft .NET family and for the future of Windows platforms. The front end design of ASP.NET is to help preserve the applications and investment of traditional ASP technology. Many ASP pages can be converted to ASP.NET without the need to change anything. There are some fundamental differences between ASP and ASP.NET, as follows.

One of the major differences is that ASP is an interpreted environment. Languages used in ASP are interpreted by the script engine and then the results are sent to the browser. Although transparent to most users, ASP.NET on the other hand is a compiled environment. All supporting languages such as C#, JScript, and VB.NET are compiled languages. This arrangement provides a fundamental framework for the .NET family to use the components of each other easily. This feature is known as the Common Language Runtime in .NET technology. Because of that, ASP.NET is not an upgrade of ASP: the software is completely new and had to be rewritten entirely. Also because of that, it is impossible to maintain 100% compatibility with ASP.

For example, we have demonstrated in ex16-05.asp how to include multiple languages in one ASP page. This feature is useful for using functions defined in other scripting libraries or engines. Due to the compiled nature and strong discipline, ASP.NET supports only one language on one page. You can, however, use multiple pages in a single application and one language on each page. Technically, this feature enables you to integrate and take advantage of multiple languages.

As a logical evolution of ASP, ASP.NET provides syntax compatibility with existing ASP pages. In other words, a large number of ASP pages (simple ones) can be converted into ASP.NET by changing the file extension to .aspx. For more complex applications, some modifications may be required. In this situation, usually only a few minor changes may be required to fix the problem.

Now, let's see how many examples in this chapter can be converted into ASP.NET directly by just changing the file extension. The results are listed in Table 16.8.

Table 16.8. Convert ASP pages to ASP.NET

ASP examples

ASP.NET

ASP examples

ASP.NET

ex16-01.asp

Yes

ex16-11.asp

Yes

ex16-02.asp

Yes

ex16-12.asp

Yes

ex16-03.asp

Yes

ex16-13.asp

Yes

ex16-04.asp

No

ex16-14.asp

Yes

ex16-05.asp

No

ex16-15.asp

Yes

ex16-06.asp

Yes

ex16-16.asp

Yes

ex16-07.asp

Yes

ex16-17.asp

Yes

ex16-08.asp

Yes

ex16-18.asp

Yes

ex16-09.asp

Yes

ex16-19.asp

Yes

ex16-10.asp

No

  


Please note that when you rename the ASP file .asp to .aspx, don't forget to change the corresponding statements in the related XHTML files.

We will explain why some of the examples cannot be converted to ASP.NET directly in section 16.5.2 and provide some guidelines for migration to ASP.NET.

16.5.2 Migrating ASP to ASP.NET with examples

As we mentioned earlier, one of the important differences between ASP and ASP.NET is that ASP.NET is a compiled environment. Therefore most of the compatibility and migration issues are concentrated on the differences between a compiled language and an interpreted language.

A compiled language usually has a strong discipline and can provide an overall description of the program structure. This is important when developing complex programs. The first thing to establish a strong discipline in programming is to request programmers to declare all variables.

In order to support the compiled nature of the .NET technology, ASP.NET employs a variable declaration checking scheme. All ASP.NET variables in C#, Visual Basic, and JScript must be defined before they can be used.

As a simple example, let's rename ex16-03.asp as ex16-20.aspx so that it is an ASP.NET page. From Table 16.8, you can see that this example works well in ASP.NET. However, the page no longer works if you comment out the variable "ii" in line 8 as illustrated in the following example:



Example: ex16-20.aspx - An ASP Example Not Working In ASP.NET

 1: <%@ Language=JScript%>
 2: <?xml version="1.0" encoding="iso-88591"?>
 3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 4:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 6: <head><title> A Not Working ASP In ASP.NET - ex1620.aspx</title><head>
 7: <body style="font-family:arial;font-weight:bold"><br /><br />
 8: <% //var ii
 9: for( ii=20;ii <=26; ii++)
10: {
11: Response.Write("<div style='font-size:" + ii + "px;color:#000000'>")
12: Response.Write("style font-size = " + ii + " px -- Hello World! </div >")
13: }
14: %>
15: </body>
16: </html>

If you request this ASP.NET page using IE6.x with the command:

http://www.pwt-ex.com/book/chap16a/ex16-20.aspx

you will get an error page as illustrated in Fig. 16.25.

Figure 16.25. Error page from ASP.NET

graphics/16fig25.jpg


From this figure, you can see that the IE6.x browser is clever enough to locate the following compilation error:



Compiler Error Message: JS1135: Variable 'ii' has not been declared

The error code is JScript 1135 (JS1135), meaning that the variable "ii" is not declared before use. The source of the error is located at line 9 and displayed in red.

Therefore, if you want your ASP page to work in ASP.NET, the first thing is to declare all variables before using them such as:

var ii, jj, kk, enObj, str, folderInfo, subFolder Using JScript

dim ii, jj, kk, enObj, str, folderInfo, subFolder Using VBScript

Another major difference comes from the CGI application. In ASP, when you request data from a query string or a form using the functions



Request.QueryString()
Request.Form()

you will have an array or a collection of strings. In ASP.NET, these two functions only return a single string containing all the options.

For example, the following code fragment defines three checkboxes:



<form action="ex1621.xxx" method="post">
       Which Operating Systems You Are Using?
  <input name="os" type="checkbox" value="Windows XP" />Windows XP<br />
  <input name="os" type="checkbox" value="Windows 2000" />Windows 2000<br />
  <input name="os" type="checkbox" value="Windows 9x" />Windows 9x<br />
  <input type="submit" value="Submit" class="butSt">
</form>

Suppose you have checked all the boxes and pressed the Submit button; the data are then passed to the program ex16-21.xxx. If this program is an ASP page, you can access the data by

Request.Form("os").Count to get the number of items in the collection

Request.Form("os")(1) to get the first item, i.e., "Windows XP"

Request.Form("os")(2) to get the second item, i.e., "Windows 2000"

If the program ex16-21.xxx is an ASP.NET page, the three functions above will generate errors, because Request.Form("os") returns a single string such as

Request.Form("os") returns "Windows XP, Windows 2000, Windows 9x"

In order to gain access to the data individually, you may need to employ some explicit methods such as GetValues() to get array access. For example, you may use

Request.Form.GetValues("os").Length to get the number of items

Response.Write(Request.Form.GetValues("os")[0] to get "Windows XP"

Response.Write(Request.Form.GetValues("os")[1] to get "Windows 2000"

Please note that ASP.NET array elements start from 0.

Now you know why example ex16-10.asp does not work by simply changing its name to ASP.NET format. In order to make it work, the first step is to rename the files:

ex16-10.htm

to

ex16-21.htm

ex16-10.asp

to

ex16-21.aspx


The second step is to modify the XHTML file ex16-21.htm as follows:



Example: ex16-21.htm - Getting Form Data With ASP.NET

 1: <?xml version="1.0" encoding="iso-88591"?>
 2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 3:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 5: <head><title> Getting Form Data With ASP.NET - ex1621.htm</title><head>
 6: <style>
 7:  .butSt{background:#aaffaa;width:250px;
 8:     font-family:arial;font-weight:bold;font-size:16pt;color:#880000}
 9:  .chkSt{background:#aaffaa;width:30px;height:30px;
10:     font-family:arial;font-weight:bold;font-size:16pt;color:#880000}
11:  .txtSt{font-family:arial;font-weight:bold;font-size:16pt;color:#ffff00}
12: </style>
13: <body style="font-family:arial;font-weight:bold;font-size:18pt;
14:   background:#000088;color:#ffff00"><br />
15:
16: <form name="statForm" action="ex16-21.aspx" method="post">
17:  <div style="position:absolute;top:40px;left:60px;">
18:        Which Operating Systems Have You Used? </div><br />
19:  <div style="position:absolute;top:100px;left:100px;">
20:    <input name="os" type="checkbox" value="Windows XP" class="chkSt" />
21:        Windows XP<br />
22:    <input name="os" type="checkbox" value="Windows 2000" class="chkSt" />
23:        Windows 2000<br />
24:    <input name="os" type="checkbox" value="Windows 9x" class="chkSt" />
25:        Windows 9x<br />
26:    <input name="os" type="checkbox" value="LINUX" class="chkSt" />
27:        LINUX<br /> </div>
28:  <div style="position:absolute;top:100px;left:340px;">
29:    <input name="os" type="checkbox" value="Mac OS" class="chkSt" />
30:       Mac OS<br />
31:    <input name="os" type="checkbox" value="IRIS" class="chkSt" />
32:       IRIS<br />
33:    <input name="os" type="checkbox" value="Solaris" class="chkSt" />
34:       Solaris<br />
35:    <input name="os" type="checkbox" value="Sun OS" class="chkSt" />
36:       Sun OS<br /> </div>
37:  <div style="position:absolute;top:260px;left:60px;">
38:    <input type="submit" value="Submit" class="butSt">
39:    <input type="reset" value="Clear" class="butSt">
40:  </div>
41: </form>
42: </body>
43: </html>

This is a simple XHTML page to define a series of checkboxes. In fact, this page is the same as ex16-10.htm except for lines 5 and 16. When this form is submitted, the form action defined in line 16 calls the ASP.NET program ex16-21.aspx. This program will process the form data using ASP.NET commands. The program code of ex16-21.aspx is listed below:



Example: ex16-21.aspx - ASP.NET Program Code

 1: <%@ LANGUAGE="JScript" %>
 2: <?xml version="1.0" encoding="iso-88591"?>
 3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 4:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 6: <head><title>ex1621.aspx</title></head>
 7: <body style="font-family:arial;font-weight:bold;font-size:18pt;
 8:     background:#000088;color:#ffff00"><br />
 9: <%
10:  var ii, tmpSt =""
11:  tmpSt = Request.Form.GetValues("os")
12:
13:  if (!tmpSt)
14:  {
15:    Response.Write("You Have Used No Operating System Before!<br />" +
16:    " I Don't Believe You!")
17:  }
18:  else if (Request.Form.GetValues("os").Length ==1)
19:  {
20:     Response.Write("You Only Use This Operating System?<br /> ")
21:     Response.Write (Request.Form.GetValues("os")[0] + "<br />")
22:     Response.Write("Try to Use More.")
23:  }
24:  else
25:  {
26:   Response.Write("You've Used All These Operating Systems? <br /><br />")
27:   for (ii=0; ii < Request.Form.GetValues("os").Length;ii++)
28:   {
29:     Response.Write(Request.Form.GetValues("os")[ii] + '<br />')
30:   }
31:   Response.Write("<br />" + "Very Good...")
32:  }
33: %>
34: </body>
35: </html>

First, we declare the variables ii and tmpSt in line 10. The tmpSt variable is used to obtain the input data from the submitted form. If you check all the boxes, tmpSt will contain a string with all your selections. If tmpSt is empty (line 13), the message in lines 1516 is displayed to indicate you haven't checked any box. If you just check one box (line 18), the ASP.NET statement in line 21



Request.Form.GetValues("os")[0]

is used to obtain only the selected item. If you have selected more than one item, the following for-loop will print out all the selections on the browser window:



for (ii=0; ii < Request.Form.GetValues("os").Length;ii++)
{
   Response.Write(Request.Form.GetValues("os")[ii] + '<br />')
}

Please note that the coding in this example concentrates on the idea of migrating ASP pages to ASP.NET. In real applications, the length of the array should not be inside a loop in order to increase efficiency. Some screen shots are shown in Figs 16.26 and 16.27.

Figure 16.26. Getting form data with ASP.NET I

graphics/16fig26.jpg


Figure 16.27. Getting form data with ASP.NET II

graphics/16fig27.jpg


Now you have some ideas on how to migrate ASP pages into ASP.NET. In fact, using the guidelines in this subsection, even some of the ASP database applications can be easily converted to ASP.NET. Databases and their applications will be discussed in the next chapter.

As a final remark, you may ask why the examples ex16-04.asp and ex16-05.asp in Table 16.8 are not working. The answer is simple: they both use Perl script. By default Perl is not supported in ASP.NET. Also the multiple language feature on a single page is not supported by ASP.NET.

    Table of Contents

    Previous Next