Home > Articles > Programming > Windows Programming

Getting Started with ASP.NET 4

  • Print
  • + Share This
Scott Mitchell introduces you to ASP.NET, shows you how to install Visual Web Developer, along with the .NET Framework and SQL Server 2008, and helps you create your first ASP.NET page.
This chapter is from the book

ASP.NET is an exciting web programming technology pioneered by Microsoft that allows developers to create dynamic web pages. Dynamic web pages are pages whose content is dynamically regenerated each time the web page is requested. For example, after you sign in, the front page of Amazon.com shows recommended products based on your previous purchases. This is a dynamic web page because it is a single web page whose content is customized based on who is visiting. This book explores how to create such dynamic web pages using ASP.NET.

ASP.NET is a robust and mature technology. ASP.NET version 1.0 was released in January 2002 and quickly became the web programming technology of choice for many. In November 2005, Microsoft released the much-anticipated version 2.0. Two years later, in November 2007, Microsoft released ASP.NET version 3.5. And ASP.NET 4 was unveiled in April 2010.

Before we create our first ASP.NET website, we need to install the .NET Framework, Visual Web Developer, and SQL Server 2008. The .NET Framework is a rich platform for creating Windows-based applications and is the underlying technology used to create ASP.NET websites.

Visual Web Developer is a sophisticated program for creating, editing, and testing ASP.NET websites and web pages. ASP.NET web pages are simple text files, meaning that you can create them using any text editor (such as Microsoft Notepad), but if you've created websites before, you know that using a tool such as Microsoft Expression Web or Adobe Dreamweaver makes the development process much easier than using a generic text editor such as Notepad. This is the case for ASP.NET, as well.

SQL Server 2008 is a database engine, which is a specialized application designed to efficiently store and query data. Many websites interact with databases; any ecommerce website, for example, displays product information and records purchase orders in a database. Starting with Hour 13, "Introducing Databases," we'll see how to create, query, and modify databases from an ASP.NET page.

This hour focuses on installing the necessary software so that we can start creating ASP.NET web applications. We create a very simple ASP.NET page at the end of this hour, but we won't explore it in any detail. We look at ASP.NET pages in more detail in the next hour and in Hour 4, "Designing, Creating, and Testing ASP.NET Pages."

What Is ASP.NET?

Have you ever wondered how dynamic websites such as 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 instance, if you have an account with Amazon.com, when you visit the home page your name is shown at the top and a list of personal recommendations is presented further down the page. When you type an author's name, a title, or a keyword into the search text box, a list of matching books appears. When you click a particular book's title, you are shown the book's details along with comments and ratings from other users. 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 whose content is determined dynamically based on user input or other information are called dynamic web pages. Any website's search engine page is an example of a dynamic web page because the content of the results page is based on the search criteria the user entered and the searchable documents on the web server. Another example is Amazon.com's personal recommendations. The books and products that Amazon.com suggests when you visit the home page are different from the books and products suggested for someone else. Specifically, the recommendations are determined by the products you have previously viewed and purchased.

The opposite of a dynamic web page is a static web page. Static web pages contain content that does not change based on who visits the page or other external factors. HTML pages, for example, are static web pages. Consider an HTML page on a website with the following markup:

<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 might exist, the output will always be the same: the text Hello, World! displayed in a bold font. The only time the content of a static web page changes is when someone edits and saves the page, overwriting the old version.

It is important to understand the differences between how a website serves static web pages versus dynamic web pages.

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 request.

Figure 1.1

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 determines what file corresponds to the requested URL and 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

To accommodate dynamic content, dynamic web pages contain source code that is executed when the page is requested (see Figure 1.2). The executing code produces the HTML that is sent back to and displayed in the visitor's browser.

Figure 1.2

Figure 1.2 The content of a dynamic web page is created by executing the dynamic web page's source code.

With this model, 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 dynamic page on August 1, 2010, 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: August 1, 2010, 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 August 1, 2008, 4:15:10 p.m.

Figure 1.2 is, in actuality, a slightly oversimplified model. Commonly, the web server and the software that executes the dynamic web page's 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—for example, an ASP.NET page—the web server hands off the responsibility of executing the page to the ASP.NET engine (see Figure 1.3).

Figure 1.3

Figure 1.3 Execution of an ASP.NET page is handled by the ASP.NET engine.

A common way that web servers determine whether the requested page is a dynamic or static web page is by the requested file's extension. For example, if the extension is .aspx, the web server knows the request is for an ASP.NET 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 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 Pages

To view an ASP.NET web page that resides on a web server, we need to request it using a web browser. The browser sends a request to the web server, which then dispatches the request to the ASP.NET engine. The ASP.NET engine processes the requested page, returns the resulting HTML to the web server, which then sends it back to the browser, where it is displayed to the user. When you're developing ASP.NET websites, the ASP.NET pages you create are saved on your personal computer. 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 specifically designed for testing ASP.NET pages. As we will see in later hours, when testing an ASP.NET page, Visual Web Developer starts the ASP.NET Development Web Server and launches a browser that issues a request of the following 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, 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. Hour 24, "Deploying Your Website," explores how to move an ASP.NET website from your personal computer to a web-hosting company's computers. After a website has been successfully deployed to a web-hosting company, you, or anyone else on the Internet, can visit the site.

  • + Share This
  • 🔖 Save To Your Account