ASP.NET and the .NET Framework


ASP.NET is part of the Microsoft .NET Framework. To build ASP.NET pages, you need to take advantage of the features of the .NET Framework. The .NET Framework consists of two parts: the Framework Class Library and the Common Language Runtime.

Understanding the Framework Class Library

The .NET Framework contains thousands of classes that you can use when building an application. The Framework Class Library was designed to make it easier to perform the most common programming tasks. Here are just a few examples of the classes in the framework:

  • File class Enables you to represent a file on your hard drive. You can use the File class to check whether a file exists, create a new file, delete a file, and perform many other file related tasks.

  • Graphics class Enables you to work with different types of images such as GIF, PNG, BMP, and JPEG images. You can use the Graphics class to draw rectangles, arcs, ellipsis, and other elements on an image.

  • Random class Enables you to generate a random number.

  • SmtpClient class Enables you to send email. You can use the SmtpClient class to send emails that contain attachments and HTML content.

These are only four examples of classes in the Framework. The .NET Framework contains almost 13,000 classes you can use when building applications.

You can view all the classes contained in the Framework by opening the Microsoft .NET Framework SDK documentation and expanding the Class Library node (see Figure 1.4). If you don't have the SDK documentation installed on your computer, then see the last section of this chapter.

Figure 1.4. Opening the Microsoft .NET Framework SDK Documentation.


Note

The Microsoft .NET Framework 2.0 includes 18,619 types; 12,909 classes; 401,759 public methods; 93,105 public properties; and 30,546 public events.


Each class in the Framework can include properties, methods, and events. The properties, methods, and events exposed by a class are the members of a class. For example, here is a partial list of the members of the SmtpClient class:

  • Properties

    • HostThe name or IP address of your email server

    • PortThe number of the port to use when sending an email message

  • Methods

    • SendEnables you to send an email message synchronously

    • SendAsyncEnables you to send an email message asynchronously

  • Events

    • SendCompletedRaised when an asynchronous send operation completes

If you know the members of a class, then you know everything that you can do with a class. For example, the SmtpClient class includes two properties named Host and Port, which enable you to specify the email server and port to use when sending an email message.

The SmtpClient class also includes two methods you can use to send an email: Send() and SendAsync(). The Send method blocks further program execution until the send operation is completed. The SendAsync() method, on the other hand, sends the email asynchronously. Unlike the Send() method, the SendAsync() method does not wait to check whether the send operation was successful.

Finally, the SmtpClient class includes an event named SendCompleted, which is raised when an asynchronous send operation completes. You can create an event handler for the SendCompleted event that displays a message when the email has been successfully sent.

The page in Listing 1.2 sends an email by using the SmtpClient class and calling its Send() method.

Listing 1.2. SendMail.aspx

<%@ Page Language="VB" %> <%@ Import Namespace="System.Net.Mail" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">     Sub Page_Load()         Dim client As New SmtpClient()         client.Host = "localhost"         client.Port = 25         client.Send("steve@somewhere", "bob@somewhere.com", _             "Let's eat lunch!", "Lunch at the Steak House?")     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Send Mail</title> </head> <body>     <form  runat="server">     <div>     Email sent!     </div>     </form> </body> </html> 

The page in Listing 1.2 calls the SmtpClient Send() method to send the email. The first parameter is the from: address; the second parameter is the to: address; the third parameter is the subject; and the final parameter is the body of the email.

Warning

The page in Listing 1.2 sends the email by using the local SMTP Server. If your SMTP Server is not enabled, then you'll receive the error An existing connection was forcibly closed by the remote host. You can enable your local SMTP Server by opening Internet Information Services, right-clicking Default SMTP Virtual Server, and selecting Start.


Understanding Namespaces

There are almost 13,000 classes in the .NET Framework. This is an overwhelming number. If Microsoft simply jumbled all the classes together, then you would never find anything. Fortunately, Microsoft divided the classes in the Framework into separate namespaces.

A namespace is simply a category. For example, all the classes related to working with the file system are located in the System.IO namespace. All the classes for working a Microsoft SQL Server database are located in the System.Data.SqlClient namespace.

Before you can use a class in a page, you must indicate the namespace associated with the class. There are multiple ways of doing this.

First, you can fully qualify a class name with its namespace. For example, because the File class is contained in the System.IO namespace, you can use the following statement to check whether a file exists:

System.IO.File.Exists("SomeFile.txt") 


Specifying a namespace each and every time you use a class can quickly become tedious (it involves a lot of typing). A second option is to import a namespace.

You can add an <%@ Import %> directive to a page to import a particular namespace. In Listing 1.2, we imported the System.Net.Mail namespace because the SmtpClient is part of this namespace. The page in Listing 1.2 includes the following directive near the very top of the page:

<%@ Import Namespace="System.Net.Mail" %> 


After you import a particular namespace, you can use all the classes in that namespace without qualifying the class names.

Finally, if you discover that you are using a namespace in multiple pages in your application, then you can configure all the pages in your application to recognize the namespace.

Note

A web configuration file is a special type of file that you can add to your application to configure your application. Be aware that the file is an XML file and, therefore, all the tags contained in the file are case sensitive. You can add a web configuration file to your application by selecting Website, Add New Item and selecting Web Configuration File. Chapter 26, "Configuring Applications," discusses web configuration files in detail.


If you add the web configuration file in Listing 1.3 to your application, then you do not need to import the System.Net.Mail namespace in a page to use the classes from this namespace. For example, if you include the Web.config file in your project, you can remove the <%@ Import %> directive from the page in Listing 1.2.

Listing 1.3. Web.Config

<?xml version="1.0"?> <configuration>     <system.web>       <pages>         <namespaces>           <add namespace="System.Net.Mail"/>         </namespaces>       </pages>     </system.web> </configuration> 

You don't have to import every namespace. The ASP.NET Framework gives you the most commonly used namespaces for free. These namespaces are

  • System

  • System.Collections

  • System.Collections.Specialized

  • System.Configuration

  • System.Text

  • System.Text.RegularExpressions

  • System.Web

  • System.Web.Caching

  • System.Web.SessionState

  • System.Web.Security

  • System.Web.Profile

  • System.Web.UI

  • System.Web.UI.WebControls

  • System.Web.UI.WebControls.WebParts

  • System.Web.UI.HTMLControls

The default namespaces are listed inside the pages element in the root web configuration file located at the following path:

\WINDOWS\Microsoft.NET\Framework\[version]\CONFIG\Web.Config 


Understanding Assemblies

An assembly is the actual .dll file on your hard drive where the classes in the .NET Framework are stored. For example, all the classes contained in the ASP.NET Framework are located in an assembly named System.Web.dll.

More accurately, an assembly is the primary unit of deployment, security, and version control in the .NET Framework. Because an assembly can span multiple files, an assembly is often referred to as a "logical" dll.

Note

The .NET Framework (version 2.0) includes 51 assemblies.


There are two types of assemblies: private and shared. A private assembly can be used by only a single application. A shared assembly, on the other hand, can be used by all applications located on the same server.

Shared assemblies are located in the Global Assembly Cache (GAC). For example, the System.Web.dll assembly and all the other assemblies included with the .NET Framework are located in the Global Assembly Cache.

Note

The Global Assembly Cache is located physically in your computer's \WINDOWS\Assembly folder. There is a separate copy of every assembly in your \WINDOWS\Microsoft.NET\Framework\v2.0.50727 folder. The first set of assemblies is used at runtime and the second set is used at compile time.


Before you can use a class contained in an assembly in your application, you must add a reference to the assembly. By default, an ASP.NET application references the most common assemblies contained in the Global Assembly Cache:

  • mscorlib.dll

  • System.dll

  • System.Configuration.dll

  • System.Web.dll

  • System.Data.dll

  • System.Web.Services.dll

  • System.Xml.dll

  • System.Drawing.dll

  • System.EnterpriseServices.dll

  • System.Web.Mobile.dll

To use any particular class in the .NET Framework, you must do two things. First, your application must reference the assembly that contains the class. Second, your application must import the namespace associated with the class.

In most cases, you won't worry about referencing the necessary assembly because the most common assemblies are referenced automatically. However, if you need to use a specialized assembly, you need to add a reference explicitly to the assembly. For example, if you need to interact with Active Directory by using the classes in the System.DirectoryServices namespace then you will need to add a reference to the System.DirectoryServices.dll assembly to your application.

Each class entry in the .NET Framework SDK documentation lists the assembly and namespace associated with the class. For example, if you look up the MessageQueue class in the documentation, you'll discover that this class is located in the System.Messaging namespace located in the System.Messaging.dll assembly.

If you are using Visual Web Developer, you can add a reference to an assembly explicitly by selecting the menu option Website, Add Reference, and selecting the name of the assembly that you need to reference. For example, adding a reference to the System.Messaging.dll assembly results in the web configuration file in Listing 1.4 being added to your application.

Listing 1.4. Web.Config

<?xml version="1.0"?> <configuration> <system.web>   <compilation>   <assemblies>   <add     assembly="System.Messaging, Version=2.0.0.0,     Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>   </assemblies>   </compilation> </system.web> </configuration> 

If you prefer not to use Visual Web Developer, then you can add the reference to the System.Messaging.dll assembly by creating the file in Listing 1.4 by hand.

Understanding the Common Language Runtime

The second part of the .NET Framework is the Common Language Runtime (CLR). The Common Language Runtime is responsible for executing your application code.

When you write an application for the .NET Framework with a language such as Visual Basic .NET or C#, your source code is never compiled directly into machine code. Instead, the Visual Basic or C# compiler converts your code into a special language named MSIL (Microsoft Intermediate Language).

MSIL looks very much like an object-oriented assembly language. However, unlike a typical assembly language, it is not CPU specific. MSIL is a low-level and platform-independent language.

When your application actually executes, the MSIL code is "just-in-time" compiled into machine code by the JITTER (the Just-In-Time compiler). Normally, your entire application is not compiled from MSIL into machine code. Instead, only the methods that are actually called during execution are compiled.

In reality, the .NET Framework understands only one language: MSIL. However, you can write applications using languages such as Visual Basic .NET and C# for the .NET Framework because the .NET Framework includes compilers for these languages that enable you to compile your code into MSIL.

You can write code for the .NET Framework using any one of dozens of different languages, including

  • Ada

  • Apl

  • Caml

  • COBOL

  • Eiffel

  • Forth

  • Fortran

  • JavaScript

  • Oberon

  • PERL

  • Pascal

  • PHP

  • Python

  • RPG

  • Scheme

  • Small Talk

The vast majority of developers building ASP.NET applications write the applications in either Visual Basic .NET or C#. Many of the other .NET languages in the preceding list are academic experiments.

Once upon a time, if you wanted to become a developer, you concentrated on becoming proficient at a particular language. For example, you became a C++ programmer, a COBOL programmer, or a Visual Basic Programmer.

When it comes to the .NET Framework, however, knowing a particular language is not particularly important. The choice of which language to use when building a .NET application is largely a preference choice. If you like case-sensitivity and curly braces, then you should use the C# programming language. If you want to be lazy about casing and you don't like semicolons, then write your code with Visual Basic .NET.

All the real action in the .NET Framework happens in the Framework Class Library. If you want to become a good programmer using Microsoft technologies, you need to learn how to use the methods, properties, and events of the 13,000 classes included in the Framework. From the point of view of the .NET Framework, it doesn't matter whether you are using these classes from a Visual Basic .NET or C# application.

Note

All the code samples in this book were written in both Visual Basic .NET and C#. The C# code samples are included on the CD that accompanies this book.





ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net