ASP.NET and the .NET Framework - The ASP Version
(Page 4 of 6 )
ASP allows the programmer to intersperse scripting code snippets within the HTML code. This scripting code can be written in a scripting language such as JavaScript or VBScript. Adding embedded script to your sample web page allows you to insert dynamic content. Modify the previous code listing, converting it to ASP, by changing the filename extension to .asp and adding VBScript to display the current time, as shown in Example 1-2. The output is shown in Figure 1-3.
Example 1-2. Code listing for HelloWorld1.asp
<% =Now%>
<html>
<body>
<h1>Hello World</h1>
<br/>
<h2>The date and time is <% =Now%>.</h2>
</body>
</html> It may not look like much, but this represents a vast improvement over static HTML. ASP allows you to create web sites full of rich and dynamic content. The scripting allows for queries, reads and writes against databases, implementation of programming logic, control of the appearance of web pages in response to user actions or returned data, and a host of other features.

Figure 1-3. Output from Example 1-2
Hello World the ASP.NET Way You will complete this evolutionary journey by changing your Hello World web page from ASP to ASP.NET. A key difference in ASP.NET is that you no longer use interpreted languages, but instead use compiled languages. Typically, ASP.NET applications are built using either C# or VB.NET. In either case, the performance will be a great improvement over script.
A significant theme of this book is that the choice between C# and VB.NET is purely syntactic. You can express any ASP.NET programming idea in either language. We suggest you write in whichever language you’re more comfortable with. The transition from VBScript to VB.NET may be slightly easier than to C#, but much of the Microsoft and third-party documentation is in C#. In this book we will show most examples in both languages, though we confess to a slight preference for C# because it is a bit more terse.
For a full exploration of VB.NET, see Programming Visual Basic .NET, Second Edition,by Jesse Liberty (O’Reilly),and for C#,see Programming C#, Third Edition by Jesse Liberty (O’Reilly).
Example 1-3 shows vbHelloWorld1.aspx in VB.NET, and Example 1-4 shows the same program in C#.
Example 1-3. Code listing for vbHelloWorld1.aspx
<%@ PAGE language="VB" %>
<%@ Page Language="VB" %>
<html>
<body>
<h1>Hello World</h1>
<h1>ASP.NET Style</h1>
<h2>Using VB .NET</h2>
<br/>
<h2>The date and time is <% =DateTime.Now( ) %>.</h2>
</body>
</html>
Example 1-4. Code listing for csHelloWorld1.aspx
<%@ PAGE language="C#" %>
<%@ Page Language="C#" %>
<html>
<body>
<h1>Hello World</h1>
<h1>ASP.NET Style</h1>
<h2>Using C#</h2>
<br/>
<h2>The date and time is <% =DateTime.Now.ToString( )
%>.</h2>
</body>
</html>
Note that the changes required to convert the ASP page to ASP.NET are minimal:
- Rename the file, changing the extension from .asp to .aspx.
- Add a first line to the code,called a page directive,which tells the compiler which language to use for all in-line code. Page directives can also be used to pass a variety of configuration settings to the compiler and will be discussed in more detail later.
- Change the script code to code written in the desired language.
The output from these changes is shown in Figure 1-4.
The ASP.NET version uses compiled code (either C# or VB.NET),which gives it a performance advantage. That advantage is meaningless in this simple example but can be very significant with larger and more complex programs.

Figure 1-4. Output from Example 1-3
Next: Hello World Using Visual Studio .NET >>
More ASP.NET Articles
More By O'Reilly Media
|
This article is excerpted from Programming ASP.NET by Jesse Liberty and Dan Hurwitz (O'Reilly, 2003; ISBN 0596004877). Check it out at your favorite bookstore today. Buy this book now.
|
|