Getting Started with ASP.NET 3.5 [con't]
|
Serving Static Web Pages
If you've developed websites before, you likely know that a website requires a web server.
A web server is a software application that continually waits for incoming web requests, which are requests for a particular URL (see Figure 1.1). The web server examines the requested URL, locates the appropriate file, and then sends this file back to the client that made the web request.
For example, when you visit Amazon.com, your browser makes a web request to Amazon.com's web server for a particular URL, say /books/index.html. Amazon.com's web server determines what file corresponds to the requested URL. It then returns the contents of this file to your browser.
This model is adequate for serving static pages, whose contents do not change. However, such a simple model is insufficient for serving dynamic pages because the web server merely returns the contents of the requested URL to the browser that initiated the request. The contents of the requested URL are not modified in any way by the web server based on external inputs.
Serving Dynamic Web Pages
With static web pages, the contents of the web page are just HTML elements that describe how the page should be rendered in the user's web browser. Therefore, when a static web page is requested, the web server can send the web page's content, without modification, to the requesting browser.
This simple model does not work for dynamic web pages, where the content of the page may depend on various factors that can differ on a per-visitor basis. To accommodate dynamic content, dynamic web pages contain source code that is executed when the page is requested (see Figure 1.2). When the code is executed, it produces HTML markup as its result, which is then sent back to and displayed in the visitor's browser.
This model allows for dynamic content because the content isn't actually created until the web page is requested. Imagine that we wanted to create a web page that displays the current date and time. To do this using a static web page, someone would need to edit the web page every second, continually updating the content so that it contained the current date and time. Clearly, this isn't feasible.
With a dynamic web page, however, the executed code can retrieve and display the current date and time. Suppose that one particular user visits this page on June 12, 2008, at 4:15:03 p.m. When the web request arrives, the dynamic web page's code is executed, which obtains the current date and time and returns it to the requesting web browser. The visitor's browser displays the date and time the web page was executed: June 12, 2008, 4:15:03 p.m. If another visitor requests this page 7 seconds later, the dynamic web page's code will again be executed, returning June 12, 2008, 4:15:10 p.m.
Figure 1.2 is, in actuality, a slightly oversimplified model. Commonly, the web server and the execution of the dynamic web page source code are decoupled. When a web request arrives, the web server determines whether the requested page is a static web page or dynamic web page. If the requested web page is static, its contents are sent directly back to the browser that initiated the request (as shown in Figure 1.1). If, however, the requested web page is dynamic\u2014for example, an ASP.NET web page\u2014 the web server hands off the responsibility of executing the page to the ASP.NET engine (see Figure 1.3).
The web server can determine whether the requested page is a dynamic or static web page by the requested file's extension. If the extension is .aspx, the web server knows the requested page is an ASP.NET web page and therefore hands off the request to the ASP.NET engine.
The ASP.NET engine is a piece of software that knows how to execute ASP.NET web pages. Other web programming technologies, such as ASP PHP and JSP have their own engines, which know how to execute ASP PHP and JSP pages.
When the ASP.NET engine executes an ASP.NET page, the engine generates the web page's resulting HTML output. This HTML output is then returned to the web server, which then returns it to the browser that initiated the web request.
Hosting ASP.NET Web Pages
To view an ASP.NET web page that resides on a web server, we need to request it through a browser. The browser sends the request to the web server, which then dis-patches the request to the ASP.NET engine. The ASP.NET engine processes the requested page and returns the resulting HTML to the browser. When you\u2019re devel-oping ASP.NET websites, the ASP.NET web pages you create are saved on your per-sonal computer. For you to be able to test these pages, then, your computer must have a web server installed.
Fortunately, you do not need to concern yourself with installing a web server on your computer. Visual Web Developer, the editor we\u2019ll be using throughout this book to create our ASP.NET websites, includes a lightweight web server specifically designed for testing ASP.NET pages locally. As we will see in later hours, when test-ing an ASP.NET page, Visual Web Developer starts the ASP.NET Development Web Server and launches a browser that issues a request of the form: http://localhost:portNumber/ASP.NET_Page.aspx.
The http://localhost portion of the request tells the browser to send the request to your personal computer\u2019s web server, in contrast to some other web server on the Internet. The portNumber specifies a particular port through which the request is made. All web servers listen for incoming requests on a particular port. When the ASP.NET Development Web Server is started, it chooses an open port, which is reflected in the portNumber portion of the URL. Finally, the ASP.NET_Page.aspx portion is the filename of the ASP.NET page being tested.
Hosting ASP.NET pages locally through the ASP.NET Development Web Server has a number of advantages:
Testing can be done while offline—Because the request from your browser is being directed to your own personal computer, you don't need to be connected to the Internet to test your ASP.NET pages.
It's fast—Local requests are, naturally, much quicker than requests that must travel over the Internet.
Advanced debugging features are available—By developing locally, you can use advanced debugging techniques, such as halting the execution of an ASP.NET page and stepping through its code line-by-line.
It's secure—The ASP.NET Development Web Server allows only local connections. With this lightweight web server, you don't need to worry about hackers gaining access to your system through an open website.
The main disadvantage of hosting ASP.NET pages locally is that they can be viewed only from your computer. That is, a visitor on another computer cannot enter some URL into her browser's Address bar that will take her to the ASP.NET website you've created on your local computer. If you want to create an ASP.NET website that can be visited by anyone with an Internet connection, you should consider using a web-hosting company.
Web-hosting companies have a number of Internet-accessible computers on which individuals or companies can host their websites. These computers contain web servers that are accessible from any other computer on the Internet. The benefits of using a web-hosting company to host your site include
A publicly available website—With a web-hosting company, any visitor who has an Internet connection can visit your website!
Use of a domain name—You can register a domain name and have it point to your website so that visitors can reach your website through a name like
www.mysite.com.Ability to focus 100% on building your website—Installing a web server, applying the latest security patches, properly configuring domain names, and so forth can be tricky tasks. By using a web-hosting company, you are paying for this service, which enables you to concentrate on building your website.
After you have settled on a web-hosting company and have set up your account, you are ready to move the ASP.NET pages from your computer to the web-hosting company. This process is referred to as deployment, and is covered in-depth in Hour 24, "Deploying Your Website." After a website has been successfully deployed, you, or anyone else on the Internet, can visit the site from their web browser.
Develop Locally, Deploy to a Web Host
Because there are different advantages for hosting a site locally versus hosting with a web-hosting company, often the best choice is to do both! I encourage you to develop, test, and debug your ASP.NET websites locally, through Visual Web Developer's built-in web server. After you have completed your site and are ready to go live, you can procure an account with a web-hosting company and deploy your site. This approach allows for the best of both worlds\u2014an ideal development environment with the end result of a publicly accessible website!
The examples and lessons presented in Hour 1 through Hour 23 are meant to be created, tested, and debugged locally. Hour 24 contains step-by-step instructions for deploying your ASP.NET website to a web-hosting company.

This chapter is an excerpt from the book, Sams Teach Yourself ASP.NET 3.5 in 24 Hours, Complete Starter Kit by Scott Mitchell, published by Sams, June 2008, ISBN 0672329972, Copyright 2008 Sams Publishing
Original: December 5, 2008




