Have you ever wondered how dynamic websites like Amazon.com work behind the scenes? As a shopper at Amazon.com, you are shown a particular web page, but the web page's content is dynamic, based on your preferences and actions. For example, if you have an account with Amazon.com, when you visit Amazon.com's home page, your name is shown at the top, and a list of personal recommendations is presented further down the page. When you type a book's title into the search text box, a list of matching books appears. When you click on a particular book's title, you are shown the book's details along with comments from other users and an overall rating. When you add the book to your shopping cart and check out, you are prompted for a credit card number, which is then billed. Web pages in websites whose content is determined dynamically based on user input or other information are called dynamic web pages. For example, any website's search engine page is an example of a dynamic web page because the content of the search page is based on the search criteria the user entered and the documents on the web server. Another example is Amazon.com's personal recommendations. The books and products that Amazon.com suggests when you view the page are different from the books and products suggested when someone else views these pages. Specifically, the recommendations at Amazon.com are generated by products you have viewed as well as previous purchases you have made. The opposite of a dynamic web page is a static web page. Static web pages contain content that does not change. HTML pages, for example, are static web pages. That is, an HTML page on a website with the following HTML markup is considered static: <html> <body> <b>Hello, World!</b> </body> </html> Such a page is considered a static web page because regardless of who views the page or what external factors there might be, the output will always be the same: the text "Hello, World!" in a bold font. The only time the content of a static web page changes is when someone edits the page and saves over the old version.
By learning ASP.NET, you will be learning how to create websites that contain dynamic web pages. It is important to understand the differences between how a website serves static web pages versus dynamic web pages.
Serving Static Web PagesIf 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. Figure 1.1. The web server handles incoming web requests.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 translates this requested URL into an actual file residing on the same computer where the web server resides. The web server returns the contents of the file, which is then rendered by your browser. This web server 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. That is, the contents of the requested URL are not modified in any way by the web server based on external inputs. Serving Dynamic Web PagesWith static web pages, the contents of the web page are just HTML elements that describe how the page should be rendered on a user's web browser. Therefore, when a static web page is requested, the web server can simply send the web page's content, without modification, to the requesting browser. This simple model won't work for dynamic web pages, where the content of the web page can depend on various factors 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 the visitor's browser. Figure 1.2. The content of a dynamic web page is created by executing the dynamic web page's source code.This model allows for dynamic content because the content for a dynamic web page isn't actually created until the web page is requested. For example, imagine that we wanted to create a web page that displays the current date and time. If we wanted to do this using a static web page, someone would need to edit the web page every second, continually updating the previous time on the page to the current time. Clearly, this isn't feasible. With a dynamic web page, source code can retrieve and display the current date and time. Say that one particular user visits this page on February 4, 2006, at 4:15:03 P.M. When the web request arrives at this time, 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 will see, displayed in the browser, the date and time: February 4, 2006, 4:15:03 P.M. If another visitor requests this page 7 seconds later, the dynamic web page's code will be executed, which will obtain the current date and time (February 4, 2006, 4:15:10 P.M.) and will return it to the requesting web browser, where it will be displayed. 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. That is, when a web request arrives, the web server determines whether the requested page is a static web page or dynamic web page. If the web page is static, the web page's 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 dynamicsay an ASP.NET web pagethe web server hands off responsibility of executing the page to the ASP.NET engine (see Figure 1.3). Figure 1.3. Execution of an ASP.NET web page is handled by the ASP.NET engine.The web server can determine if the requested page is a dynamic or static web page by the requested file's extension. If the extension is .aspx, then the web server knows the requested page is an ASP.NET web page and, therefore, hands off the request to the ASP.NET engine.
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 the HTML to the browser that initiated the web request. Hosting ASP.NET Web PagesAfter we've created ASP.NET web pages, to be able to view them, we need to request the ASP.NET page through a web browser. The browser then sends a request to the web server, which dispatches the request to the ASP.NET engine. The engine processes the page and returns the resulting HTML markup to the browser. When you're developing ASP.NET websites, the ASP.NET web pages you create will be saved on your personal 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'll be using throughout this book to create our ASP.NET websites, includes a lightweight web server designed for testing ASP.NET pages locally. As we will see in later hours, when testing an ASP.NET page, Visual Web Developer starts its 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's web server, as opposed 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:
The main disadvantage of hosting ASP.NET pages locally is that they can be viewed only from your own computer. That is, a visitor on another computer cannot enter some URL into his browser's Address bar that will visit 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. A web-hosting company has a number of Internet-accessible computers for individuals or companies to place their websites. These computers contain web servers that are accessible from any other computer on the Internet. When setting up an account, you can ask the company to register a domain name for you. A domain name is the text that a person would enter into the web browser to visit your website. (For example, the domain name of Microsoft's site is microsoft.com. For my personal website, I might choose scottmitchellinfo.com.)
The benefits of using a web-hosting company to host your site include
After your web-hosting account is set up, you can move web pages to the computer where your account is hosted. Then you, or anyone else on the Internet, can visit these pages. If you upload a web page named index.htm and your domain name was bobshomepage.org, anyone could view the index.htm page by typing http://www.bobshomepage.org/index.htm into the browser's Address bar. Getting Started with a Web-Hosting CompanyBefore you can create an account with a web-hosting company, you need to find a company that supports ASP.NET version 2.0. The easiest way to find such a company, in my opinion, is to check sites like TopHosts.com and HostIndex.com, which list thousands of web-hosting companies. You can search these sites' databases for web-hosting companies that meet certain criteria, such as cost per month, geographical location, web server platforms used, and other criteria. Costs for web-hosting companies can range from a few dollars per month to hundreds or thousands of dollars per month, based on the features provided. Additionally, most web-hosting companies have a setup fee in the $25 to $100 range. On top of the web-hosting costs, you will probably want to register a domain name, which typically costs between $10 and $35 per year, depending on the domain name registrar used. After you have picked out a web-hosting company and have double-checked that it supports ASP.NET development, contact the company's sales staff to create an account. Develop Locally, Deploy to a Web HostBecause there are many advantages to hosting a site both locally and 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 for visitors, then procure an account with a web-hosting company and deploy your site. This approach allows for the best of both worldsan ideal development environment with the end result of a publicly accessible website!
|