Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

S E S S I O N

29

Migrating from ASP to ASP.NET

Session Checklist

Understanding the major changes in syntax and function for ASP.NET

Identifying major areas of risk in migrating your applications

Understanding how ASP and ASP.NET can coexist

This session provides an overview of the challenges you will face as you migrate your applications from ASP to ASP.NET.

ASP and ASP.NET Compatibility

After installing the .NET Framework in your existing ASP environment, you will find that you are able to continue running your ASP pages without modification. Additionally, as you begin to write your own ASP.NET pages you will find that they operate nicely side by side with your existing ASP pages. However, as you begin to migrate code from your ASP pages to ASP.NET you will find that there are at least four major areas of differences in how you coded in ASP versus how you will code in ASP.NET:

You can only script in one language per page or user control.

Page rendering your content through function calls is no longer supported.

You must define your functions as script blocks.

There are syntax differences and language modifications.

292

Sunday Afternoon

Scripting language limitations

When creating ASP pages, developers will commonly use VBScript, JScript, PerlScript, or even Python. While the use of scripting languages is fine given their ease of use, they do have several disadvantages over compilable languages such as C#, VB .NET, or C++. The primary disadvantage is performance. Every time an ASP page is requested, the page is interpreted by the relevant scripting engine and cached to increase performance. This is done through an in-memory cache, so at any time only a relatively small number of ASP pages can be effectively cached. The second major disadvantage is that scripting languages are often designed for accomplishing procedurally oriented tasks in a quick and effective manner. This often means that they lack the full functionality and extensibility that a C# or VB .NET language would support.

In ASP.NET, pages are generated with languages such as VB .NET, C++, and C#. ASP.NET is not limited to these languages, however, as ASP.NET pages can be created with any compiler that can generate .NET Common Language Runtime (CLR)-compliant code. Additionally once this code is compiled, regardless of the language, it is stored as Microsoft Intermediate Language (MSIL) in the form of an executable (.exe) or dynamic-link library (.dll) file. When the code is executed on a client system, it undergoes a final round of Just In Time (JIT) compilation to transform it into machine-specific instructions. Theoretically, this means that there are no performance differences between language selections in terms of final delivered performance! Code written for the .NET platform runs under the control of the CLR. Language selection under ASP.NET is thus a lifestyle choice rather than a decision based upon expected performance, integration with the API, or other factors. All languages have access to and support the same base set of classes and functionality, and all execute to the same performance standards.

The CLR has been designed to replace the existing runtime layers of COM, Microsoft Transaction Services (MTS), and COM+.

Note

In ASP, you could readily mingle VBScriptand JScript-coded functions in the same page. In ASP.NET, VBScript has been replaced by VB .NET, which provides a range of syntax modifications that you will need to address. Although JScript is still supported, most JScript developers will prefer to use C#. So, from a core code migration perspective, you should examine any of your existing ASP pages that incorporate more than one language on the server side.

Note

While you can no longer mix languages on the server side, you can include a client-side scripting language that is interpreted by the client browser and is different from what is used on the server side. For instance, you could have all of your business logic written in VB .NET and compiled on the server side, and have client-side code written in JavaScript.

Session 29—Migrating From ASP to ASP.NET

293

Rendering HTML page elements

In order to manipulate the display properties of an HTML control in ASP, you would intermingle scripting values with the HTML of the control to modify font sizes, colors, and other properties of the HTML element — the final code would then be rendered appropriately and sent to the end user’s browser. In ASP.NET, although you can render directly to the browser, you should focus instead on using object-oriented techniques to modify the property values and execute the methods of the ASP.NET server controls.

Listing 29-1 shows an example of an ASP page written to display a textbox with a black background, gray text, 20pt font, and a defined text value.

Listing 29-1 Using ASP to render an HTML control

<%@Language = “VBScript”%> <%

Sub DisplayTextBox(forecolor, backcolor, value, fontsize) Response.Write(“<input type=’text’ name=’txtDisplay’ value=’” & value & “‘“) Response.Write(“style=color:” & forecolor & “;background-color:”& backcolor &

“;font-size:” & fontsize & “pt;>”)

Response.Write(“</textbox>”) End Sub

%>

<HTML>

<BODY>

<%DisplayTextBox “gray”,”black”, “Hello World”,”20” %> </BODY>

</HTML>

The DisplayTextBox function is called after the <body> tag has been rendered, and the subroutine accepts the forecolor, backcolor, value, and fontsize attributes passed in the function call. The DisplayTextBox then effectively generates a series of strings that are subsequently rendered in the browser.

In ASP.NET, you will need to establish script blocks, and implement structured functions, which can then manipulate the control directly through the control’s inherent properties and methods. This is illustrated in Listing 29-2.

Listing 29-2 Using ASP.NET to render an HTML control

<HTML>

<%@ Page Language=”VB” Debug=”False” Trace=”False” %> <HEAD>

<SCRIPT LANGUAGE=”vb” RUNAT=”server”>

Sub Page_Load(sender as Object, e as EventArgs) Dim oColor as System.Drawing.Color

UpdateTextBox(oColor.gray,oColor.black,”Hello World”, 20) End Sub

Function UpdateTextBox(forecolor, backcolor, value, fontsize) Dim oColor as System.Drawing.Color

Continued

294

Sunday Afternoon

Listing 29-2

Continued

txtDisplay.Text = value txtDisplay.ForeColor = forecolor txtDisplay.BackColor = backcolor

txtDisplay.Font.Size = FontUnit.Point(fontsize) End Function

</SCRIPT>

</HEAD>

<BODY>

<FORM RUNAT=”server” ID=”Form1”>

<ASP:TEXTBOX ID=”txtDisplay” RUNAT=”server” /> </FORM>

</BODY>

</HTML>

In this example, the UpdateTextBox function is called each time the page is rendered and passes the requisite parameters similar to what occurred in the ASP example. The UpdateTextBox function, however, operates very differently — instead of the developer modifying the output stream rendered in the browser directly, the developer works with the properties and methods of the TextBox to obtain the desired results.

In both the ASP and the ASP.NET examples, the formatted output and the rendered HTML are nearly identical, both generating a similar looking HTML 3.2-compliant rendered output.

<input name=”txtDisplay” type=”text” value=”Hello World” id=”txtDisplay” style=”color:Gray;background-color:Black;font-size:20pt;” />

Using script blocks

In ASP.NET, there is a radical shift away from supporting the unstructured, interpreted script code that was typically written in procedural terms. In ASP, during each page execution, the code begins processing the first line of code and then works its way down. This encouraged ASP developers to intermingle HTML code with the scripting logic to produce a mess of spaghetti code without good separation of logic and presentation layers.

The impact of the move to script blocks means that the code segment shown in Listing 29-3 will no longer work.

Listing 29-3 Mixing VBScript and HTML in ASP

<%@Language = “VBScript”%> <HTML>

<HEAD>

<%Function CreateTable()%> <TABLE>

<%For x = 1 to 10%> <TR>

<TD>

<%Response.Write “Cell Number” & x%> </TD>