If you ever thought about using ASP.NET, or wondered why you should, this article is for you. It will show you how ASP.NET fits in with other Internet technologies, and explain the advantages that ASP.NET offers over alternative technologies. It is excerpted from chapter three of ASP.NET Web Development with Macromedia Dreamweaver MX, written by Costas Hadjisotiriou et al. (Apress, 2004; ISBN: 1590593480)
IN THIS CHAPTER, you will look beyond the three-letter acronyms that form ASP.NET to discover its origins and how it fits in the Internet technologies picture. We will show you the advantages that ASP.NET offers over alternative technologies. We will also compare it to its predecessor, classic ASP. This will ready you for Chapter 4, where we will delve deeper into the technical aspects of ASP.NET and provide you with a basic migration guide from classic ASP.
If you are familiar with ASP.NET and feel ready to get started creating sites in ASP.NET utilizing Dreamweaver’s facilities, you can safely skip this chapter, although you may find some of the information presented here useful.
The .NET Framework
To understand why ASP.NET exists at all, you first need to examine the concept of the .NET Framework. The purpose of .NET is quite simple: to create a common framework in which all managed applications will run. For those of you who have worked with Java, this may sound very familiar. In fact, .NET seems to have been inspired by the Java success, but instead of trying to be a common language for different systems, .NET is focused on bringing different programming languages together to operate uniformly on Windows.
As a result, the obvious disadvantage is that .NET applications can only run on Windows systems, whether they act as servers or clients. For web developers interested in ASP.NET, this is probably a nonissue because all the .NET activity is on the server and only HTML reaches the client. The advantages of .NET over its nearest equivalent, Java, include the following:
Because Java is multiplatform, its scope is limited to the lowest-common denominator of available computers and operating systems. In contrast, .NET programs can take full advantage of any feature of Windows.
Because Java is the lowest-common denominator, developers often use proprietary classes to access features available to the target platform. This leads to nonportable code that has to be customized for the target platform anyway.
Because Java Database Connectivity (JDBC) forces round-trips to the database, the .NET application architecture is faster. ADO.NET disconnected data access allows more data functions to be completed in memory.
In this chapter, we explain the basics of how .NET works so that ASP.NET makes sense. Figure 3-1 shows a schematic of the logical layers of the .NET Framework and where ASP.NET fits into it.
Figure 3-1.The .NET Framework's logical layers
Regardless of which language you use to write your application, it is translated into intermediate code, which is this code that will be executed in real time by the Common Language Runtime (CLR). This intermediate code is generated by the .NET compilers, and it is called Microsoft Intermediate Language (MSIL). It is a CPU-independent set of instructions that can be efficiently converted to CPU-specific code by a just-in-time (JIT) compiler. Because the CLR supplies one or more JIT compilers for each computer architecture it supports, the same set of MSIL can be compiled and run on any supported architecture.
TIP As you can see from Figure 3-1, ASP.NET is simply another component operating on top of the same architecture as any Windows Forms .NET program. The implication is that any code that handles business logic or other background programming can be easily reused for both desktop and web applications.
Because of the independence provided by the CLR, developers do not have to worry about missing any features or functionality arising from your language choice.
Another important way .NET affects web developers is it contains features that prevent bad coding practices that create nonreusable components. .NET encourages separation of the logical parts that compose a web page: interface design, business rules, data manipulation functions, custom objects, and so on. More on this in the “Active Server Pages .NET” section of this chapter and of course in subsequent chapters.
Installing the .NET Framework
To install the .NET classes necessary for ASP.NET development, you need to download the Microsoft .NET Framework version 1.1 redistributable from http://msdn.microsoft.com/library/default.asp? url=/downloads/list/netdevframework.asp. It’s a 24MB download, but it contains everything you need for ASP.NET web pages or Windows Forms programming. You can also download the .NET software development kit (110MB) from the same page, but it’s not essential unless you want to develop XML web services. The redistributable package is available as a stand-alone executable file, Dotnetfx.exe, which you simply double-click after downloading. You need to have administrator privileges to install it.
You can install Dotnetfx.exe on any machine that runs Windows 98 or later; however, ASP.NET is supported only on the following platforms:
Microsoft Windows 2000 Professional (Service Pack 3 recommended)
Microsoft Windows 2000 Server (Service Pack 3 recommended)
Microsoft Windows XP Professional
Microsoft Windows Server 2003 family
The platforms just listed are the only ones capable of hosting and testing ASP.NET applications. However, you can develop these ASP.NET applications on any computer running Dreamweaver, including the Macintosh.
You also need to have
Microsoft Data Access Components 2.6; Microsoft Data Access Components 2.7 Service Pack 1 is recommended. This is for applications that use data access.
Internet Information Services (IIS) version 5.0 or later. To access the features of ASP.NET, IIS with the latest security updates must be installed prior to installing the .NET Framework.
After installation and restarting, you don’t need to do anything else. ASP.NET is installed as part of your IIS web server.
This section is intended for those of you who are relatively new to web development. You are working with the latest technology available, but you still need some information to understand why ASP.NET is what it is. You may skip this section if you have experience with ASP or other web scripting technologies, but some of the information presented here may be new and interesting to you.
Common Gateway Interface
Sharing information on the Internet is a simple process: A client machine uses a web browser to request a file from a web server, and the web server responds by sending the requested file. Improvements to that model include generating HTML dynamically by using the output of a program, which is executed on behalf of the web client. This is the purpose of the Common Gateway Interface (CGI): to execute a program on behalf of the web server (and by extension, the web client) and merge the output into the response.
ISAPI Filters
The Internet Server Application Programming Interface (ISAPI) model was developed as a higher-performance alternative to CGI. ISAPI provides a number of advantages over CGI, including lower overhead, faster loading, and better scalability.
The chief difference between the CGI and ISAPI programming models is the way processing is handled. With CGI, the system creates a unique process for every request. Each time an HTTP server receives a request, it initiates a new process. Because the operating system must maintain all these processes, CGI is resource intensive. This inherent limitation makes it difficult to develop responsive Internet applications with CGI. With ISAPI, requests do not require a separate process. Threads are used to isolate and synchronize work items, resulting in a more efficient use of system resources.
Because ISAPI applications are compiled code, they are processed faster than ASP files or files that call COM components.
Internet Information Server
On Windows systems, the default software that coordinates all the components of an HTTP request is the IIS server. IIS, along with the HTTP listener (HTTP.sys) handles the following tasks:
Establishes and maintains HTTP connections
Populates instances of the ASP and ASP.NET objects with available data from each HTTP request, such as session state information, error information, and client certificate information
Passes a request body through a chain of possible destinations, as described in Table 3-1
Populates an instance of the response object as a response is built during the processing of each request
Modifies HTTP headers in a response according to configuration settings set by the administrator or page-level settings written by the application developer
Sends HTTP responses back to clients
Table 3-1. Functions Performed by IIS
Request Type
Action Taken by IIS
HTML page
IIS returns the page immediately in HTML format.
ISAPI extension
IIS loads the ISAPI DLL (if it is not already running) and the request is sent to the extension.
File name whose extension is mapped to a particular ISAPI extension
IIS loads the appropriate DLL file and presents the request to the extension. For example, requests for Active Server Pages (*.asp) files are resolved by invoking an ASP-specific extension module namedasp.dll. Likewise, requests for ASP.NET resources (for example,*.aspx,*.asmx,*.ashx) are mapped toaspnet_isapi.dll so that all requests for files with those extensions will be directed toaspnet_isapi.dll. The.stmand.shtmextensions are mapped tossinc.dll.
CGI application
IIS creates a new process. IIS then provides the query string and other parameters that are included with the request through the environment and standard input (STDIN) handle for the process.
Most of the non–Microsoft-related tasks just listed are also performed by many other kinds of web server software. However, we are only concerned with IIS as our Internet enabler. Later, we will take you through the creation of a web site in IIS, and that will put these concepts in perspective.
Layers in Web Request Processing
It’s instructive to look at how all the components we’ve mentioned so far fit together. Today’s client/server applications resemble their linearly programmed ancestors so little that they’ve been given a new name: the multi-tier application, also known as n-tier architecture. In this model, processing is distributed between the client and the server, and business logic is captured in a middle tier. Most systems perform the following three main tasks, which correspond to three tiers, or layers, of the n-tier model. Table 3-2 describes the three tiers.
Table 3-2.Multi-Tier Architecture Layers
Task
Tier
Description
User interface and navigation
Tier 1
A graphical user interface so that users can interact with the application, input data, and view the results of requests. This layer is also responsible for formatting the data once the client receives it back from the server. In web applications, a browser performs the tasks of this layer.
Business logic
Tier 2
Components that connect the data sources with the presentation application. They are reusable pieces of code that can be changed without the need to change the whole application. Their tasks are logical functions, security checks, calculations, and so on.
Data services
Tier 3
Data services are provided by a structured (databases, XML databases) or unstructured (email programs, directories) data store, which manages and provides access to the data.
Figure 3-2 shows how the Microsoft classic ASP technologies fit in this system architecture.
Figure 3-2. Classic ASP and IIS
The three-tier architecture isolates each major piece of ASP and component functionality so that the presentation (user interface) is independent of the processing rules and business logic, which in turn is separate from the data. This model requires much more analysis and design up front, but it greatly reduces maintenance costs and increases functional flexibility in the long run via reuse, easy maintenance, sectional upgrade, and so on. Of course, this is a Windows-based concept, but the principles are platform independent, which is why it has proved popular with most modern web projects. Let’s look at how ASP.NET uses the second tier to link the scripts, many more components, and server under one shell.
ASP and Other Web Scripts
Microsoft Active Server Pages can be thought of as a type of ISAPI extension whose code is executed in real time (a script). More specifically, ASP is a server-side scripting environment that you can use to create dynamic and interactive web pages and build powerful web applications. With ASP, you can combine HTML pages, script commands, and COM components to create interactive web pages or powerful web-based applications that are easy to develop and modify.
When the server receives a request for an ASP file, it processes server-side script code contained in the file to build an HTML web page that is sent to the browser. In addition to server-side script code, ASP files can contain HTML (including related client-side scripts) and calls to COM components that perform a variety of tasks, such as connecting to a database or processing business logic.
Unlike conventional CGI applications, ASP was designed to simplify the process of developing web applications. By placing a few ASP tags in the appropriate places, you can add database connectivity or advanced customization features to a static page. ASP uses popular web scripting languages such as Microsoft JScript (the Microsoft implementation of the ECMA 262 language specification), Microsoft Visual Basic Scripting Edition (VBScript), or any COM-compliant scripting language, including JavaScript, Perl, and others.
This interactivity is valid for most other web scripting technologies. From the web programmer’s point of view, ASP may appear to be more intuitive than other scripted technologies by bringing the server execution to an HTML page (others, like PHP, also use the ASP paradigm); however, markup tags get dispersed with programming pieces, which can lead to code that is hard to read. In contrast, languages like Perl use code statements to create the page output, which is clearer but also creates code that looks more like a program than a web page.
The easiest way to introduce ASP.NET is to highlight the most fundamental difference from ASP. Even if you have not used ASP before, you will be able to follow the general idea—we simply contrast the concept, not the technicalities.
The Request/Response Model
Recall that in web pages, you send an HTTP request (for example, a URL and the data in a form) to the server and it sends back a response (a web page). Your browser then typically displays that response. When you click various web page elements, such as buttons or links, one of two things can happen. If the web page has client-side script code (downloaded with the web page, typically in JScript or JavaScript), an event handler might handle the event without even talking to the server. Or, depending on the HTML code for the web page element (such as a Submit button), your browser might send an HTTP request to the server.
The programming model for classic ASP is based on the HTTP request/response model. This programming model can be fairly difficult to use because you have to save data across HTTP requests and responses so that you can reconstruct the state of a page at every request. This contrasts with the simpler event-driven forms and controls model that most Windows applications use. For example, if you had a wizard-like series of pages in your web application, you would have to submit all the accumulated data to each page as hidden inputs, whereas a desktop application can simply access the data anytime.
The ASP.NET Forms-Based Model
The programming model in ASP.NET adds forms and controls. The programming model embodied by ASP.NET Web Forms is fairly similar to that of a Windows application, although the mechanisms underneath the programming models are very different.
Conceptually, a pure Web Forms application consists of a set of forms that contain controls and HTML. The controls have properties and methods, and they can generate events. These events are usually handled on the server, although it’s possible to handle certain events on the client. This is a much simpler model than the ASP model because you don’t have to think about how the browser will reconstruct the state of the page—ASP.NET takes care of that. You only have to create the logic of interaction between the controls of a web page and the logic between web pages.
The abilities of ASP.NET are also augmented by access to a whole new range of components, making it more like a true desktop application. The three-tier schematic then looks as shown in Figure 3-3.
Figure 3-3.The ASP.NET position within the Windows environment
More About ASP.NET
There are many more aspects of ASP.NET that are of interest to web developers.
Not just a script: ASP.NET is not just the ASP version that can run within .NET. As mentioned before, it can be written using up to 20 languages, in addition to the 4 built-in ones: VB .NET, C#, C++, and JScript .NET. A list of ongoing projects on .NET ports of various languages can be found at http://www.cetus-links.org/oo_dotnet.html#oo_dotnet_netlang. ASP.NET also allows you to take full advantage of the features of the CLR, such as type safety, inheritance, language interoperability, and versioning.
More built-in controls: Classic ASP was easy to use and made the creation of dynamic web content easy, but as the complexity of the dynamic content increased, the code necessary to enable complex pages became unmanageable. ASP.NET simplifies the creation of complex pages by providing prebuilt server-side controls and object types that web developers can use much like the drag-and-drop controls that developers use in Windows desktop applications.
Real-time configuration: There’s no need to restart the web server anymore. ASP.NET is configured via easy-to-read XML files that are editable with any text editor and that take effect the moment they’re saved. The same is true for components that are used in your pages—as soon as the DLL file is copied, the .NET Framework takes care of installation and registration so that it’s immediately available for use inside ASP.NET pages.
Advanced browser handling: Because ASP.NET writes the HTML for ASP.NET controls, you don’t have to worry about writing different HTML for different browsers. Instead, ASP.NET detects what browser is being used and generates the appropriate HTML. All you do is use the high-level controls and let ASP.NET deal with the details. (This does not, however, get you out of testing your application against all the browsers you intend to support.)
Easy programming model: You don’t have to worry about re-creating the state of each control in your page for each request. You can concentrate on writing code for how the controls can interact and ASP.NET handles the presentation to web clients.
Rich class framework: Application features that used to be hard to implement or required a third-party component can now be added in just a few lines of code using the .NET Framework. The .NET Framework offers over 4,500 classes that encapsulate rich functionality like XML, data access, file upload, regular expressions, image generation, performance monitoring and logging, transactions, message queuing, SMTP mail, and so on.
Compiled execution: ASP.NET dynamically compiles your pages and stores the compiled results so they can be reused for subsequent requests. ASP.NET will also automatically detect any changes you make and recompile them in real time. Dynamic compilation ensures that your application is always up to date, and compiled execution makes it fast. Most applications migrated from classic ASP are three to five times faster in pages served.
Rich output caching: ASP.NET output caching can dramatically improve the performance and scalability of your application. When output caching is enabled on a page, ASP.NET executes the page just once and then saves the result in memory, in addition to sending it to the user. When another user requests the same page, ASP.NET serves the cached result from memory without reexecuting the page. Output caching is configurable and can be used to cache individual regions or an entire page.
Web farm session state: ASP.NET session state lets you share session data and user-specific state values across all machines in a cluster of web servers, known as a web farm. Users who were once directed to the server holding their session’s state now can hit different servers in the web farm over multiple requests and still have full access to their session.
Memory leak, deadlock, and crash protection: ASP.NET automatically detects and recovers from errors like deadlocks and memory leaks to ensure that your application is always available to your users. ASP.NET automatically starts another copy of the ASP.NET worker process if the old one is running an application with memory leak problems, and it directs all new requests to the new process. Once the old process has finished processing its pending requests, it is gracefully disposed of and the leaked memory is released.
Easy deployment: ASP.NET dramatically simplifies installation of your application. With ASP.NET, you can deploy an entire application as easily as an HTML page: Just copy it to the server. No need to run regsvr32 to register any components, and configuration settings are stored in an XML file within the application.
Easy migration path: You don’t have to migrate your existing applications to start using ASP.NET. ASP.NET runs on IIS side-by-side with classic ASP on Windows 2000 and Windows XP platforms. Your existing ASP applications continue to be processed by asp.dll, whereas new ASP.NET pages are processed by the new ASP.NET engine. We will show you the details of this in the Appendix.
XML web services: XML web services allow applications to communicate and share data over the Internet, regardless of operating system or programming language. ASP.NET makes exposing and calling XML web services simple. Any class can be converted into an XML web service with just a few lines of code, and any class can be called by any Simple Object Access Protocol (SOAP) client. Likewise, ASP.NET makes it incredibly easy to call XML web services from your application. No knowledge of networking, XML, or SOAP is required.
Mobile web device support: ASP.NET Mobile Controls is a new group of server controls that work at a more abstract level than regular ASP.NET controls. Mobile Controls let you easily target cell phones and PDAs (including over 80 mobile web devices) using ASP.NET. The list of supported devices is also easily updatable via configuration files, and like everything .NET, it is completely extensible to cover any future type of device. You write your application just once and the mobile controls automatically generate WAP/WML, HTML, CHTML, or your custom XML language, as required by the requesting device.
Now that you know where ASP.NET fits into the web developer’s radar screen, let’s get busy with the details. Next we discuss how an ASP.NET web site is created in Dreamweaver and IIS.
Dreamweaver uses web site information to keep track of changes to files, updates to hyperlinks in documents, file uploads, and so on. What it does not do is to act as a web server—that is the role of IIS.
Dreamweaver Setup
Creating an ASP.NET web site is just like creating any other type of site in Dreamweaver. Follow these steps to set up a test site.
Go to the Files panel and click Manage Sites, as shown in Figure 3-4.
Figure 3-4.Starting the Site Definition Wizard in Dreamweaver The Manage Sites dialog box appears. Click the New button and select Site from the small menu. The Site Definition Wizard appears, in which you can specify your web site’s properties.
If it is not already selected, click the Basic tab and type a name for your web site—we will use testASPNETsite for our example. Select a language, as shown in Figure 3-5.
Figure 3-5. Choosing a technology for the new site
If you can’t see the .NET language you want, select any one. In ASP.NET it’s just a matter of changing the @ Page directive’s language attribute. We’ll show you how to do that in the next chapter.
Click Next to define the directory where you want to store your files. Dreamweaver detects the presence of IIS and allows you to use it as the testing server. Dreamweaver’s web site definition is only internal—your pages will not be published on the web by merely existing under a Dreamweaver web site. We show you how to set up IIS in the next section. For now, go through the remaining screens of the wizard and specify where the testing server is located, and use the wizard’s Advanced tab to customize the site creation. When you are done, your new web site should appear in the Files panel. (Ours is shown in Figure 3-6 with one page. You can use the default.aspx page supplied in the download.)
Figure3-6.The newly created site, as listed in the Dreamweaver Files panel
To set up a page as the default for the web site, right-click it and select Set as Home Page from the menu. This will make it the page that appears if no specific page is requested.
Let’s see how a site’s contents can be made available to the web.
Internet Information Services Setup
To publish your content (to the Internet or an internal network), you will need to enter the server’s IP address in the URL box of your browser. However, if you are just using your local machine as your server, you can enter localhost or 127.0.0.1 instead. You eventually want to see the following result when calling your page—we used an imaginary IP address in Figure 3-7.
Figure 3-7. The desired result after successfully setting up the web site in IIS
Open the IIS interface on your Windows server by clicking Start -> Programs -> Administrative Tools -> Internet Services Manager. A web site is created called Default Web Site. This corresponds to any files you place directly in the C:\inetpub\wwwroot directory. If you are hosting only one web site on your server, you might as well use this predefined site. Otherwise, you can add new ones via the New Web Site Creation Wizard. The Web Site Creation Wizard can be started by selecting Action -> New -> Web Site, as shown in Figure 3-8.
Figure 3-8. Starting the Web Site Creation Wizard in IIS
The first screen asks for the web site name, so enter the name you used in Dreamweaver. The next screen asks for your site’s IP address. If you don’t know it, leave it unspecified. You use the Host Header field (shown in Figure 3-9) when you want to host multiple web sites on the same server. The server needs some way of distinguishing which web site each received request is meant for, so it looks up the value of the HTTP Host Header. If you’re hosting only one web site on your server (or if you are using your local machine), leave this field blank.
Figure 3-9.Specifying the IP address and the domain of your web site
Specify the physical directory where your web site content is stored, just as you did for Dreamweaver, as shown in Figure 3-10.
Figure 3-10. Specifying the path to your web site’s root
Remember, this can be anywhere on your machine, not just under C:\Inetpub\wwwroot, as long as web sharing is enabled for that folder. Let’s open a bracket here and quickly see how this is done.
Right-click your folder and click Properties. Select the Web Sharing tab and choose the Share this folder option, as shown in Figure 3-11. Then specify the web site you are sharing it on, if necessary.
Figure 3-11. Configuring any folder for web access
Back to the IIS wizard, accept the defaults in the screen where web site permissions are set. The remaining features are advanced settings that involve security risks.
Click Finish to save the web site setup. Next you’ll set your default page. Right-click the newly created web site and click Properties, as shown in Figure 3-12.
Figure 3-12. Additional web site properties
On the Documents tab, you can add the names of the default documents that will be sent to the client browser if none were specified. Let’s assume that for your ASP.NET web site, the default is a page called default.aspx, so click Add and type that name. Then use the arrows on the left of the page list to move it up in priority (or remove the other entries).
While the Properties dialog box is open, let’s check to see if the ASPX pages will be handled properly. Click the Home Directory tab and click the Configuration button. This changes application-related settings for this web site. Click the Mappings tab to display a list of all the ISAPIs that are listening for requests to the page types that they handle. If your installation of the .NET Framework was successful, you will see the extension .aspx in that list and the path to the aspnet_isapi.dll library that processes ASP.NET requests. You don’t need to modify anything here, it’s just reassuring that the mapping is actually there!
Your creation is now viewable to the world! Test your web site by typing the URL and pressing Enter.
http://[your IP address or domain name]/testASPNETsite
Your page should look as shown earlier in Figure 3-7.
Almost all aspects of .NET, and by extension ASP.NET, can be customized via XML configuration files. These are readable (but not writeable) from .NET applications, so you can extend their applicability by defining custom configuration sections and parameters.
For an ASP.NET web site, configuration is achieved by placing a file with the correct syntax, named web.config, at the web site directory. You can add more web.config files in child directories that you want to configure more specifically. For example, if you wanted to deny access to only a certain part of your web site, your web.config file might contain
The <browserCaps> section controls the settings of the browser capabilities class, System.Web.HttpBrowserCapabilities. It is contained directly within the <system.web> section.
<configuration> <system.web> <browserCaps>
For example, the following sample web.config demonstrates parsing the USER_AGENT HTTP header for the platform type. It uses a regular expression to match portions of text of the user agent string with known systems. If you have used ASP before, you will recognize that the file specifies name/value pairs in the form of assignment statements, similar to the IIS browscap.ini files. For example, the line platform=WinNT sets the value of the platform field to the string WinNT.
Within the ASP.NET page’s code, we could also examine the value of the variables platform, browser, version, and so on, and make decisions regarding the content we display.
If Request.Browser.Platform="Win95" Then Response.Write("Your platform cannot host .NET applications<BR>") End If
By this point you might be getting impatient to see more ASP.NET code. The next chapter is full of it!
Summary
We covered a lot of theory and concepts in this chapter. It is time to look at some practical examples of applying ASP.NET technology using Dreamweaver to solve everyday web programming problems. In the meantime, if you would like to see who can start hosting your ASP.NET web sites, take a look at http://www.asp.net/Default.aspx?tabindex=8&tabid=40, where you will also find links to various ASP.NET community sites.