Hello World the ASP.NET Way

You willcomplete 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.

Note

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 , by Dave Grundgeiger (O’Reilly), and for C#, see Programming C#, by Jesse Liberty (O’Reilly).

Example 2-3 shows vbHelloWorld1.aspx in VB.NET, and Example 2-4 shows the same program in C#.

Example 2-3. Code listing for vbHelloWorld1.aspx

<%@ 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 2-4. Code listing for csHelloWorld1.aspx

<%@ 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:

  1. Rename the file, changing the extension from .asp to .aspx.

  2. 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.

  3. Change the script code to code written in the desired language.

The output from these changes is shown in Figure 2-3.

Output from Example 2-3

Figure 2-3. Output from Example 2-3

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 morecomplex programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216