A journey of a thousand miles begins with a single step.
-Lao Tzu
ASP.NET is a Web development platform that provides the programming model and software infrastructure necessary to build enterprise-class applications. Although largely syntax compatible with its popular predecessor—Active Server Pages (ASP)—ASP.NET is a revolutionary new programming framework designed to enable the rapid development of Web applications and Web services. As part of the Microsoft .NET platform, ASP.NET provides a component-based, extensible, and easy-to-use way to build, deploy, and run Web applications that target any browser or mobile device.
ASP.NET is supported on a variety of platforms, including Microsoft Windows 2000 with at least Service Pack 2, Windows XP Professional, and Windows Server 2003. To develop ASP.NET server applications, Internet Information Services (IIS) version 5.0 or later is also required. Other software you need—for example, Microsoft Data Access Components (MDAC) 2.7—is automatically installed when you set up the .NET Framework.
ASP.NET fits perfectly in the trend that has shifted the focus of development from client/server applications to Web-based architectures and Internet-deployed applications. It is the next step in the evolution and a giant step indeed. ASP.NET is the summation of Web development technologies that rapidly followed one after another in the past five years—one building over another, and each filling the gaps of its predecessor. As a result, ASP.NET is currently the most technologically advanced, feature-rich, and powerful platform for building distributed applications transported by the HTTP protocol. The Web Forms model is simply a blanket term that figuratively describes the abstraction model ASP.NET constructs atop the evergreen, old, and faithful HTTP protocol.
Prior to the advent of ASP.NET, three main technologies and platforms were available to develop Web applications: ASP, Java Server Pages (JSP), and the open source Web platform commonly referred to as LAMP (Linux plus Apache plus MySQL plus either Perl, Python, or PHP as the programming language).
Note |
For completeness, we should also mention a couple of platform-specific, lower-level technologies that ASP and JSP rely on. ASP is actually an Internet Server Application Programming Interface (ISAPI) extension, whereas JSP is implemented as a special servlet application. ISAPI extensions on IIS-based platforms and servlets on Java-based systems let you create server-side, Web-deployed applications using a more classic approach. You write a module that builds and renders the page rather than declaratively design the page using a mix of markup text and embedded code. |
Although each has language-specific and architecture-specific features, all these Web development platforms are designed to create interactive pages as part of a Web-based application. To some extent, all enable developers to separate programming logic from the page layout through the use of components that the page itself is responsible to call and render. Aside from this common ultimate goal, significant differences exist among those platforms, most of which relate to the programming model and languages they promote and support. For example, JSP exploits the Java framework of classes and, with JavaBeans, provides an effective extensibility model for reusing components. In addition, JSP supports tag customization and lets developers associate code with a custom tag definition. Finally, because it's a key element of the Java 2 Enterprise Edition (J2EE) platform, JSP relies on the Java language, a first-class language as opposed to the scripting languages used by both ASP and LAMP platforms. So how does ASP.NET fit in exactly?
Like ASP and other Web development environments, ASP.NET also works on top of the HTTP protocol and takes advantage of HTTP commands and policies to set up two-way, browser-to-server communication and cooperation. What really differentiates ASP.NET from the plethora of other Web development technologies—and what makes it especially revolutionary—is the abstract programming model it propounds, the Web Forms model. In addition, the whole ASP.NET platform comes as a native part of the Microsoft .NET Framework. To be sure you grasp the importance of this last point, let me explain. ASP.NET applications are now compiled pieces of code, are made of reusable and extensible components, can be authored with first-class languages (including C#, Microsoft Visual Basic .NET, JScript .NET, and J#), and can access the entire hierarchy of classes in the .NET Framework.
In short, ASP.NET combines the best of all worlds. It is syntax compatible (and to a large extent, also language compatible) with ASP. It provides the same object-oriented features as JSP applications (tag customization, first-class compiled languages, components, extensibility, and reusability). And as icing on the cake, ASP.NET delivers a wealth of goodies, tools, and powerful system features that can be effectively grouped within the blanket expression tools for abstracting the HTTP programming model. Lots of programmer-friendly classes let you develop pages using typical desktop methods. The Web Forms model promotes an overall event-driven approach, but it's deployed over the Web and has the potential to target any vendor's platform through the support of XML.
Note |
From a technical point of view, ASP.NET adopts the best of the ASP and JSP programming models. Conversely, it doesn't inherit much from the LAMP platform in terms of technical aspects. The appeal of LAMP is that it excels as a robust and extensible (because it is open-source) platform with flourishing and proactive grassroots community sites and Web boards. |
Whichever way you look at ASP.NET, it's a quantum leap beyond any other Web programming style you might have experienced in the past. In this chapter, we'll present an overview of the ASP.NET platform, introducing topics such as the Web Forms model, structure of Web pages, and tasks involved when building ASP.NET applications. In Chapter 2, "Web Forms Internals," we'll drill down into the architectural elements of the Web Forms model, and then bear off into the sea of ASP.NET features and widgets. So get ready, and let the show begin.
A key point I need to explain so that you understand the rationale behind the ASP.NET Web Forms model is that there is a need to provide a better strategy to deal with the strong and growing demand for cheap Web interaction. As a matter of fact, the HTTP protocol, especially its stateless nature, creates a sort of bottleneck for the development of Web applications. On the other hand, the inherent simplicity of HTTP is the key to its worldwide adoption and effectiveness—in short, we probably couldn't have the Internet as we know it without a protocol like HTTP. Yet, as demand increases, programmers have to devise better ways of setting up easy and effective communication from the client to the server and vice versa.
Various techniques have been experimented with over time to smooth the communication across different pages and across multiple invocations of the same page. Programmers are used to thinking in terms of a client-generated action that results in a server-side reaction. Such a basic and fundamental pattern simply cannot be accomplished, at least not literally, over the Web. A certain degree of abstraction and some system-provided services are needed to make smooth communication happen. Although they aren't perfect, JSP and ASP let programmers target any browser while coding against a relatively familiar and understandable model. ASP, much more than JSP, thinks declaratively and has quite a slim and scanty object model. Overall, programmers who become Web programmers are forced to adopt a different mindset and toss the familiar action/reaction paradigm out the door.
With Web Forms, the event-driven model of interaction finally comes to the Web. Implementing an event model over the Web requires any data related to the client-side user's activity to be forwarded to the server for corresponding and stateful processing. For the model to work, the server needs to process the output of client actions and trigger reactions while being aware of the overall state of the application. The state of the application contains two types of information: the state of the client and the state of the session. The state of the client is easily accessible through the syntax and the implementation of the
HTML element. But what about the overall state of the session?
As mentioned, HTTP is a stateless protocol, which means two successive requests across the same session are resolved by newly instantiated environments in which no session-specific information is maintained, except all the information the application itself might have stored in global objects. In ASP, reentrant forms are a common way to work around such a system limitation. A reentrant form is an HTML element that posts to the same page that contains it. Reentrant forms alone do not fully solve the issue. However, by combining them with code blocks and hidden fields storing state information that is critical for the page, many developers elegantly overcame the obstacle.
What was once an ASP best-practice has been standardized and integrated in the ASP.NET runtime to become the key feature that endows ASP.NET applications with automatic state maintenance. The ASP.NET runtime carries the page state back and forth across page requests. When generating HTML code for a given page, ASP.NET encodes and stuffs the state of server-side objects into a few hidden, and transparently created, fields. When the page is requested, the same ASP.NET runtime engine checks for embedded state information—the hidden fields—and utilizes any decoded information to set up newly created instances of server-side objects. The net effect of such a mechanism is not unlike the Windows Forms model on the desktop and is summarized in Figure 1-1.
Figure 1-1: Comparing the Windows Forms and Web Forms models in the .NET Framework.
The Windows Forms model stems from the typical event-driven desktop programming style. No matter what connectivity exists between the client and server components, the server always works in reaction to the client's input. The server is aware of the overall application state and operates in a two-tier, connected manner. The Web Forms model needs some machinery to support the same event-driven programming model. In Figure 1-1, the needed machinery is represented by the state deserialization that occurs when the page is requested and the state serialization performed when the HTML response is being generated.
In charge of this filtering work is the ASP.NET HTTP runtime—a piece of code that extends and specializes the overall capabilities of the hosting Web server. Reentrant forms and hidden fields are the low-level tools used to perform the trick. Such a model wouldn't be as effective without a back-end, rich object model spanning the whole content of the server page. Crucial to the building and effective working of the ASP.NET development platform is the component model. The ASP.NET component model is shared with all other types of .NET applications—Windows Forms, console applications, and Web services.
The ASP.NET object model is a branch of the .NET Framework and is designed to provide a server-side counterpart to virtually any HTML page elements, such as HTML tags like and and page-wide tags like
and
Pro ASP.NET (Ch 01)
Blank lines in the preceding listing separate the three sections—directives, code, and page layout. Notice the unsparing use of the runat attribute—it's one of the most important pieces of the whole ASP.NET jigsaw puzzle. Later in this chapter, we'll discuss runat in more detail. For now, it suffices to say that the runat attribute promotes an otherwise lifeless server-side tag to the rank of a component instance. Let's quickly review the code.
Thanks to the runat attribute, the input text box becomes an instance of the HtmlInputControl class when the page is processed on the server. The Value property of the class returns the current content of the control. This value is converted to uppercase and then assigned to the InnerText property of the server-side control used to render an HTML tag. When the user clicks the Submit button, the page automatically posts back to itself. This time, the magic is performed by the runat attribute set for the
tag. Once on the server, the current value of the text box is read and automatically assigned to a newly created instance of the HtmlInputControl. Next, the code associated with the OnServerClick event runs. The code works on controls whose state has been restored and updated with client information. When the MakeUpper event handler completes, the page is ready for rendering. At this point, updated HTML code is sent to the browser.
To test the page, copy the .aspx file to your Web server's root directory. Normally, this is c:inetpubwwwroot. If you want, create an ad hoc virtual directory. Next, point the browser to the page. Figure 1-2 shows what you get.
Figure 1-2: Our first ASP.NET page in action.
No matter how simple you keep a test ASP.NET page, a lot of complex features and techniques are always involved. This page is no exception. In the following chapters, you'll learn more about server-side controls and the difference between HTML and Web controls. You'll also see how to deploy real applications and not just individual pages. Now that we've dirtied our hands with some ASP.NET code, let's step back and review the layers that actually make an ASP.NET page.
Directives configure the run-time environment that will execute the page. In ASP, only one directive was supported—@Language—and you had to place it on the very first line of the page. The following code shows an ASP directive:
<% @Language="VBScript" CodePage="1232" %>
Several new directives have been added to ASP.NET and, at the same time, their usage has been made easier and dependent on less strict conditions. In ASP.NET, directives can be located anywhere in the page, although it's a good and common practice to place them at the beginning of the file. In addition, the name of a directive is case-insensitive and the values of directive attributes don't need to be quoted.
Tip |
The ASP @Language directive is no longer officially supported in ASP.NET. The same goal is accomplished by the Language attribute within the @Page directive. However, for migration purposes, ASP.NET treats the old-style @Language directive block as an @Page directive with the Language attribute set. As a result, the ASP-style syntax shown below is still accepted: <% @Language="VBScript" CodePage="1232" %> It gets silently translated into the following code: <% @Page Language="VBScript" CodePage="1232" %> The ASP.NET @Page directive supports the same set of processing directives as ASP—CodePage, EnableSessionState, LCID, Transaction—plus a few more. |
The most important and most frequently used directive in ASP.NET is @Page. The complete list of ASP.NET directives is shown in Table 1-1.
Directive |
Description |
---|---|
@ Assembly |
Links an assembly to the current page or user control |
@ Control |
Defines control-specific attributes that guide the behavior of the control compiler |
@ Implements |
Indicates that the page, or the user control, implements a specified .NET Framework interface |
@ Import |
Indicates a namespace to import into a page or user control |
@ OutputCache |
Controls the output caching policies of a page or user control |
@ Page |
Defines page-specific attributes that guide the behavior of the page compiler and the language parser that will preprocess the page |
@ Reference |
Links a page or user control to the current page or user control |
@ Register |
Creates a custom tag in the page or the control. The new tag (prefix and name) is associated with the namespace and the code of a user-defined control |
With the exception of @Page and @Control, all directives can be used both within a page and a control declaration. @Page and @Control are mutually exclusive. @Page can be used only in .aspx files, while the @Control directive can be used only in .ascx files.
Note |
As we'll see in more detail in Chapter 10 and Chapter 18, ASP.NET features two types of user-defined controls. Custom controls are controls that derive from existing .NET Framework classes. User controls (also known as pagelets) are smaller and simpler ASP.NET pages that can be embedded in host pages and programmatically driven through methods and properties. Custom controls are created as assemblies and have a .dll extension. User controls are source files, similar to ASP.NET pages, but they have the .ascx extension. |
We'll cover the @Control, @OutputCache, and @Register directives in detail in Chapter 10. In the meantime, let's learn more about the other, more page-specific, directives.
The syntax of a processing directive is unique and common to all supported types of directives. Multiple attributes must be separated with blanks, and no blank can be placed around the equals sign (=) that assigns a value to an attribute, as the following line of code demonstrates:
<%@ Directive_Name attribute="value" [attribute="value"...] %>
Each directive has its own closed set of typed attributes. Assigning a value of the wrong type to an attribute, or using a wrong attribute with a directive, results in a compilation error.
Important |
The content of directive attributes is always rendered as plain text. However, attributes are expected to contain values that can be rendered to a particular type. When the ASP.NET page is parsed, all the directive attributes are extracted and stored in a dictionary. The names and number of attributes must match the expected schema for the directive. The string that express the value of an attribute is valid as long as it can be converted into the expected type. For example, if the attribute is designed to take a Boolean value, "true" or "false" are its only acceptable values. |
The @Page Directive
The @Page directive can be used only in .aspx pages and generates a compile error if used with other types of ASP.NET pages such as controls and Web services. Each .aspx file is allowed to include at most one @Page directive. Although not strictly necessary from the syntax point of view, the directive is realistically required by all pages of some complexity.
@Page features about 30 attributes that can be logically grouped in three categories: compilation (Table 1-2), overall page behavior (Table 1-3), and page output (Table 1-4). Each ASP.NET page is compiled upon first request, and the HTML actually served to the browser is generated by the methods of the dynamically generated class. Attributes in Table 1-2 let you fine-tune parameters for the compiler and choose the language to use.
,Pro ASP.NET (Ch 01)Click the View|Source menu item...
Part I - Building an ASP.NET Page
Part II - Adding Data in an ASP.NET Site
Part III - ASP.NET Controls
Part IV - ASP.NET Application Essentials
Part V - Custom ASP.NET Controls
Part VI - Advanced Topics
Index