About the Authors Glenn Johnson Glenn Johnson is a professional trainer, consultant, and developer whose experience spans the past 20 years. As a consultant and developer, he has worked on several large projects, the latest being a successful conversion of a SmallTalk/GemStone system to C#/ Microsoft SQL Server for a very large customer. This is Glenn’s third .NET-related book, and he has also developed courseware for and taught classes in many countries on Microsoft ASP.NET, Visual Basic .NET, C#, and the .NET Framework. Glenn holds the following Microsoft Certifications: MCT, MCPD, MCTS, MCAD, MCSD, MCDBA, MCP + Site Building, MCSE + Internet, MCP + Internet, and MCSE. You can find Glenn’s Web site at http://GJTT.com. Tony Northrup Tony Northrup, MCTS, MCSE, CISSP, and Microsoft MVP, is a consultant and author. He has written more than a dozen books covering Windows networking, security, and develop ment. Among other titles, Tony is coauthor of the MCSA/MCSE Self-Paced Training Kits for Exams 70-536 and 70-330/340. When he’s not consulting or writing, Tony enjoys cycling, hik ing, and nature photography. Tony lives in Phillipston, Massa chusetts, with his wife, Erica, his cat, Sam, and his dog, Sandi. You can learn more about Tony by visiting his Web site at http://www.northrup.org.
Acknowledgments Glenn Johnson To Jenny Moss Benson, thanks for your constructive feedback throughout the entire process of writing this book. Thanks for also having patience while the summer months were passing and my desire to play outweighed my desire to write. Thanks to Ken Jones for persuading me to have a co-author on this book. Tony Northrup, thanks for being a wonderful co-author, and thanks for your profes sionalism during all stages of writing this book. To everyone at Microsoft Press who has played a role in getting this book to the public, thank you for your hard work and thanks for making this book venture a positive experience for me. Tony Northrup Many people helped me with this book by distracting me at the right times. My friends and family, especially Tara, John, and Emilie Banks; Kristin Casciato; Bob and Heather Dean; Mike, Michelle, Ray, and Sandi Edson; Chris and Diane Geggis; Bob Hogan; Sam Jackson; Tom and Heather Keegan; Kim Lively; Jenny Lozier; Eric and Ali son Parucki; Skip and Chris Rice; Scott and Debbie Robichaud; Carol Whitney; and Jimmy and Gloria Young helped me enjoy my time away from the keyboard. More than anyone, I have to thank my wife, Erica, for being so patient during many long days of writing. xxv
Introduction This training kit is designed for developers who plan to take Microsoft Certified Technical Specialist (MCTS) exam 70-528, as well as for developers who need to know how to develop applications using the Microsoft .NET Framework 2.0. We assume that before you begin using this kit, you have a working knowledge of Microsoft Windows and Microsoft Visual Basic or C#. By using this training kit, you’ll see how to do the following: ■ Create a Web application using Web server controls, event handlers, application state, and session state. ■ Create custom Web server controls. ■ Develop accessible Web applications that can be used by a global audience. ■ Integrate a Web application with a back-end database. ■ Create a Web application that stores user-specific information and preferences. ■ Add authentication and authorization features to your application to improve security and add multiple access levels. ■ Create Web applications that can be used from mobile phones and PDAs.Hardware Requirements The following hardware is required to complete the practice exercises: ■ Computer with a 600 MHz or faster processor ■ 192 MB of RAM or more ■ 2 GB of available hard disk space ■ DVD-ROM drive ■ 1,024 x 768 or higher resolution display with 256 colors ■ Keyboard and Microsoft mouse, or compatible pointing device xxvii
xxviii IntroductionSoftware Requirements The following software is required to complete the practice exercises: ■ One of the following operating systems: ❑ Microsoft Windows 2000 with Service Pack 4 ❑ Microsoft Windows XP with Service Pack 2 ❑ Microsoft Windows XP Professional x64 Edition (WOW) ❑ Microsoft Windows Server 2003 with Service Pack 1 ❑ Microsoft Windows Server 2003, x64 Editions (WOW) ❑ Microsoft Windows Server 2003 R2 ❑ Microsoft Windows Server 2003 R2, x64 Editions (WOW) ❑ Microsoft Windows Vista ■ Microsoft Visual Studio 2005 (A 90-day evaluation edition of Visual Studio 2005 Professional Edition is included on DVD with this book.)Using the CD and DVD A companion CD and an evaluation software DVD are included with this training kit. The companion CD contains the following: ■ Practice tests You can reinforce your understanding of how to create .NET Framework 2.0 applications by using electronic practice tests that you customize to meet your needs from the pool of Lesson Review questions in this book. Or, you can practice for the 70-528 certification exam by using tests created from a pool of 300 realistic exam questions, which is enough to give you many different practice exams to ensure that you’re prepared. ■ Code Most chapters in this book include sample files associated with the lab exercises at the end of every lesson. For some exercises, you will be instructed to open a project prior to starting the exercise. For other exercises, you will create a project on your own and be able to reference a completed project in the event you experience a problem following the exercise. ■ An eBook An electronic version (eBook) of this book is included for times when you don’t want to carry the printed book with you. The eBook is in Portable Doc ument Format (PDF); you can view it by using Adobe Acrobat or Adobe Reader.
Introduction xxix The evaluation software DVD contains a 90-day evaluation edition of Visual Studio 2005 Professional Edition, in case you want to use it with this book.How to Install the Practice Tests To install the practice test software from the companion CD to your hard disk, do the following: 1. Insert the companion CD into your CD drive, and accept the license agreement. A CD menu appears. NOTE If the CD menu doesn’t appear If the CD menu or the license agreement doesn’t appear, AutoRun might be disabled on your computer. Refer to the Readme.txt file on the CD-ROM for alternate installation instructions. 2. Click the Practice Tests item, and follow the instructions on the screen.How to Use the Practice Tests To start the practice test software, follow these steps: 1. Click Start | All Programs | Microsoft Press Training Kit Exam Prep. A window appears that shows all the Microsoft Press training kit exam prep suites installed on your computer. 2. Double-click the lesson review or practice test that you want to use. NOTE Lesson reviews vs. practice tests Select the (70-528) Microsoft .NET Framework 2.0—Web-Based Client Development lesson review to use the questions from the “Lesson Review” sections of this book. Select the (70-528) Microsoft .NET Framework 2.0 — Web-Based Client Development practice test to use a pool of 300 questions similar to those in the 70-528 certification exam. Lesson Review Options When you start a lesson review, the Custom Mode dialog box appears so that you can configure your test. You can click OK to accept the defaults, or you can customize the number of questions you want, how the practice test software works, which exam objectives you want the questions to relate to, and whether you want your lesson review to be timed. If you’re retaking a test, you can select whether you want to see all the questions again or only those questions you missed or didn’t answer.
xxx Introduction After you click OK, your lesson review starts. ■ To take the test, answer the questions and use the Next, Previous, and Go To but tons to move from question to question. ■ After you answer an individual question, if you want to see which answers are correct—along with an explanation of each correct answer—click Explanation. ■ If you’d rather wait until the end of the test to see how you did, answer all the questions, and then click Score Test. You’ll see a summary of the exam objectives you chose and the percentage of questions you got right overall and per objec tive. You can print a copy of your test, review your answers, or retake the test. Practice Test Options When you start a practice test, you choose whether to take the test in Certification Mode, Study Mode, or Custom Mode: ■ Certification Mode Closely resembles the experience of taking a certification exam. The test has a set number of questions, it’s timed, and you can’t pause and restart the timer. ■ Study Mode Creates an untimed test in which you can review the correct answers and the explanations after you answer each question. ■ Custom Mode Gives you full control over the test options so that you can cus tomize them as you like. In all modes, the user interface you see when taking the test is the basically the same, but with different options enabled or disabled depending on the mode. The main options are discussed in the previous section, “Lesson Review Options.” When you review your answer to an individual practice test question, a “References” section is provided that lists where in the training kit you can find the information that relates to that question; it also provides links to other sources of information. After you click Test Results to score your entire practice test, you can click the Learn ing Plan tab to see a list of references for every objective.
Introduction xxxiHow to Uninstall the Practice Tests To uninstall the practice test software for a training kit, use the Add Or Remove Pro grams option in Windows Control Panel.How to Install the Code To install the sample files referenced in the book’s exercises from the companion CD to your hard disk, do the following: 1. Insert the companion CD into your CD drive, and accept the license agreement. A CD menu appears. NOTE If the CD menu doesn’t appear If the CD menu or the license agreement doesn’t appear, AutoRun might be disabled on your computer. Refer to the Readme.txt file on the CD-ROM for alternate installation instructions. 2. Click the Code item, and follow the instructions on the screen. The code will be installed to Documents and Settings<user>My Documents MicrosoftPress70-528.Microsoft Certified Professional Program The Microsoft certifications provide the best method to prove your command of cur rent Microsoft products and technologies. The exams and corresponding certifica tions are developed to validate your mastery of critical competencies as you design and develop, or implement and support, solutions with Microsoft products and tech nologies. Computer professionals who become Microsoft-certified are recognized as experts and are sought after industry-wide. Certification brings a variety of benefits to the individual and to employers and organizations. MORE INFO All the Microsoft certifications For a full list of Microsoft certifications, go to http://www.microsoft.com/learning/mcp/default.asp.
xxxii IntroductionTechnical Support Every effort has been made to ensure the accuracy of this book and the contents of the companion CD. If you have comments, questions, or ideas regarding this book or the companion CD, please send them to Microsoft Press by using either of the following methods: E-mail: email@example.com Postal Mail: Microsoft Press Attn: MCTS Self-Paced Training Kit (Exam 70-528): Microsoft .NET Framework 2.0 - Web- Based Client Development Editor One Microsoft Way Redmond, WA 98052–6399 For additional support information regarding this book and the CD-ROM (includ ing answers to commonly asked questions about installation and use), visit the Microsoft Press Technical Support website at http://www.microsoft.com/learning/ support/books/. To connect directly to the Microsoft Knowledge Base and enter a query, visit http://support.microsoft.com/search/. For support information regard ing Microsoft software, please connect to http://support.microsoft.com.Evaluation Edition Software Support The 90-day evaluation edition provided with this training kit is not the full retail prod uct and is provided only for the purposes of training and evaluation. Microsoft and Microsoft Technical Support do not support this evaluation edition. Information about any issues relating to the use of this evaluation edition with this training kit is posted to the Support section of the Microsoft Press Web site (http:// www.microsoft.com/learning/support/books/). For information about ordering the full version of any Microsoft software, please call Microsoft Sales at (800) 426-9400 or visit http://www.microsoft.com.
Chapter 1Introducing the ASP.NET 2.0 WebSite Microsoft Visual Studio 2005 and ASP.NET 2.0 represent a major release for Microsoft. If you have previous experience with Visual Studio products, you will see the differences immediately when you attempt to create your first Web site. Even if you are new to Visual Studio 2005 and ASP.NET 2.0, you will be able to immediately take advantage of the productivity enhancements. This chapter starts by introducing the Web site players (Web server, Web browser, and Hypertext Transfer Protocol [HTTP]). It explores the architecture of an ASP.NET Web site and then shows the various ways that you can create a Web site. After that, you will learn about some of the Web site configuration options in Visual Studio 2005. Exam objectives in this chapter: ■ Program a Web application. ❑ Avoid performing unnecessary processing on a round trip by using a page’s IsPostBack property. ■ Create and configure a Web application. ❑ Create a new Web application. ❑ Add Web Forms pages to a Web application. ■ Configure settings for a Web application. ❑ Configure system-wide settings in the Machine.config file. ❑ Configure settings for a Web application in the application’s Web.config file. ❑ Manage a Web application’s configuration by using the Web Site Adminis tration Tool. ■ Optimize and troubleshoot a Web application. ❑ Troubleshoot a Web application by using ASP.NET Trace. 1
2 Chapter 1 Introducing the ASP.NET 2.0 Web Site Lessons in this chapter: ■ Lesson 1: Understanding the Players . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ■ Lesson 2: Creating a Web Site and Adding New Web Pages . . . . . . . . . . . . . . . 17 ■ Lesson 3: Working with Web Configuration Files . . . . . . . . . . . . . . . . . . . . . . . 31 ■ Lesson 4: Using ASP.NET Trace to Explore Web Pages . . . . . . . . . . . . . . . . . . . 37Before You Begin To complete this chapter, you must: ■ Be familiar with Microsoft Visual Basic or C#. ■ Have Microsoft Windows XP, Internet Information Services (IIS) 5.1, and Visual Studio 2005 installed with Microsoft SQL Server 2005 Express Edition. ■ Be familiar with the Visual Studio 2005 Integrated Development Environment (IDE). ■ Understand how to make assemblies available to other applications. ■ Have a basic understanding of Hypertext Markup Language (HTML) and client- side scripting. Real World Glenn Johnson It’s easier to learn how to develop Web clients once you understand who the players are. I have seen many people attempt to learn Web development without learning the roles of the Web browser, HTTP, and the Web server. It’s not a pretty sight.
Lesson 1: Understanding the Players 3Lesson 1: Understanding the Players It’s important to get an understanding of the roles of the Web server, Web browser, and HTTP before starting your Web development. The typical communication pro cess can be generalized into the following steps: 1. The Web browser initiates a request for a Web server resource. 2. HTTP is used to send the GET request to the Web server. 3. The Web server processes the request. 4. The Web server sends a response to the Web browser. HTTP protocol is used to send the HTTP response to the Web browser. 5. The Web browser processes the response, displaying the Web page. 6. The user enters data and performs an action, such as clicking a Submit button that causes the data to be sent back to the Web server. 7. HTTP is used to POST the data back to the server. 8. The Web server processes the data. 9. The Web server sends the response back to the Web browser. 10. HTTP is used to send the HTTP response to the Web browser. 11. The Web browser processes the response, displaying the Web page. This section gives a brief description of how the Web browser exchanges communica tions with the Web server via HTTP. It also describes the responsibilities of both the Web browser and Web server. After this lesson, you will be able to: ■ Describe the Web server’s role in responding to requests for resources. ■ Describe the Web browser’s role in collecting and presenting data to the user. ■ Describe HTTP’s role in communicating to the Web server. ■ Describe how HTTP verbs are used to request resources from the Web server. ■ Describe the status-code groups that are implemented in HTTP. ■ Describe Distribute Authoring and Versioning. ■ Describe PostBack, the common method of sending data to the Web server. ■ Describe some methods for troubleshooting HTTP. Estimated lesson time: 30 minutes
4 Chapter 1 Introducing the ASP.NET 2.0 Web SiteThe Web Server’s Role Let’s start with the Web server. The original Web servers were responsible for receiving and handling requests from the browsers via HTTP. Each Web server handled the request and sent a response back to the Web browser. After that, the Web server closed the connection and released all resources that were involved in the request. All resources were released because the Web server needed to be able to handle thou sands of requests per minute, and the original Web pages were simple, static HTML pages. The Web environment was considered to be “stateless” because no data was held at the Web server between Web browser requests, and because the connection was closed after the response was sent (see Figure 1-1). Web Browser Web Server Client initiates communications with page request GET Default.html 1. Process request Display Page 2. Send response Server responds and close the with page connection Figure 1-1 A simple request/response between Web browser and Web server in a stateless environment. Today’s Web servers deliver services that go far beyond the original Web servers. In addition to serving static HTML files, the Web servers can also handle requests for pages that contain code that will execute at the server; the Web servers will respond with the results of code execution, as shown in Figure 1-2. Web servers also have the ability to store data across Web page requests, which means that Web pages can be connected to form Web applications. Because many Web sites are set up as Web appli cations containing many Web pages, the idea of a Web server delivering a single page to the Web browser and closing the connection is rather outdated. Web servers now implement “keep alive” features for connections that make the Web servers keep the connections to the Web browsers open for a period of time with anticipation of addi tional page requests from a Web browser.
6 Chapter 1 Introducing the ASP.NET 2.0 Web Site typically sent between the Web server and Web browser using port 80, or, when using secure HTTP (HTTPS), port 443. MORE INFO HTTP/1.1 Specification For more information on HTTP/1.1, see the HTTP/1.1 specification at http://www.w3.org/Protocols/ rfc2616/rfc2616.html. When a Web page is requested, a textual command like the following is sent to the Web server: GET /default.aspx HTTP/1.1 Host: www.northwindtraders.com Notice that the first line contains the method, also known as a verb or a command, called GET, and is followed by the Uniform Resource Locator (URL) of the Web page to be retrieved, which is followed by an indicator of the HTTP version to be used. The method indicates what action is to be performed by the Web server using the URL that follows the method. Table 1-1 contains a list of some of the common HTTP meth ods with a description of their uses. Note that, if Distributed Authoring and Versioning (DAV) is enabled on the Web site, many more verbs will be available, such as LOCK and UNLOCK. The second line identifies the name of the host that may be used by the Web server if the Web server is hosting more than one Web site. This process is known as using host headers to identify the Web site that will handle the request(s). Table 1-1 Common HTTP/1.1 Methods HTTP Description Method OPTIONS Used by client applications to request a lists of all supported verbs. Checks to see if a server allows a particular verb before wasting net work bandwidth trying to send an unsupported request. GET Gets a URL from the server. A GET request for a specific URL, say, /test.htm, retrieves the test.htm file. Data retrieved using this verb is typically cached by the browser. GET also works with collections, such as those in directories that contain collections of files. If you request a directory, the server can be configured to return a default file, such as index.html, that may be representative of the directory.
Lesson 1: Understanding the Players 7Table 1-1 Common HTTP/1.1 Methods HTTP Description Method HEAD Retrieves the meta information for a resource. This information is typ ically identical to the meta information sent in response to a GET request, but the HEAD verb never returns the actual resource. The meta information is cacheable. POST Used to create a new, dynamically named resource. Data retrieved using this verb is typically not cached. PUT Allows a client to directly create a resource at the indicated URL on the server. The server takes the body of the request, creates the file specified in the URL, and copies the received data to the newly cre ated file. If the file exists and is not locked, the content of the file will be overwritten. DELETE Used to delete a resource at the Web server. Requires write permis sions on the directory. TRACE Used for testing or diagnostics; allows the client to see what is being received at the other end of the request chain. Responses to this method are never cached. CONNECT Reserved for use with a proxy that can dynamically switch to being a tunnel, such as Secure Socket Layer (SSL) protocol. DEBUG Is not defined in the HTTP/1.1 specification, but is used to start ASP.NET debugging. Informs Visual Studio 2005 of the process that the debugger will attach to.Notice that Web-browser-to-Web-server communication is referred to as a request. InASP.NET, the Request object represents the Web browser’s communications to theWeb server asking for a resource. What Is Distributed Authoring and Versioning? Distributed Authoring and Versioning (DAV) is a set of extensions to HTTP/1.1 that simplifies Web site development when working in a team scenario. DAV is an open standard and is available on numerous platforms. DAV provides the ability to lock and unlock files plus the ability to designate versions.
8 Chapter 1 Introducing the ASP.NET 2.0 Web Site DAV is built directly on HTTP/1.1, so no other protocols, such as File Transfer Pro tocol (FTP) or Server Message Block (SMB), are required. DAV also provides the ability to query the Web server for various resource properties such as file names, time stamps, and sizes. DAV also gives the developers the ability to perform server-side file copying and moving. For example, you can use the HTTP GET and PUT verbs to retrieve files from the Web servers and save them to different loca tions, or you can use the DAV’s COPY verb to simply tell a server to copy the file. The communication from the Web server back to the Web browser is commonly referred to as the response. In ASP.NET this is represented as the Response object. When the Web server responds to a request, the communication is typically in the fol lowing text-based format: HTTP/1.1 200 OK Server: Microsoft-IIS/6.0 Content-Type: text/html Content-Length: 38 <html><body>Hello, world.</body><html> The first line contains the protocol and version information, plus a status-code and reason. The three-digit status codes are grouped as shown in Table 1-2. Exam Tip Even if you don’t memorize every status code, it’s helpful to know the five status- code groupings in Table 1-2. Table 1-2 Status-Code Groups Status-Code Description Group 1xx Informational: Request received, continuing to process. 2xx Success: The action was successfully received, understood, and accepted. 3xx Redirect Command: Further action must be taken in order to com plete the request. 4xx Client Error: The request has a syntax error or the server does not know how to fulfill the request. 5xx Server Error: The server failed to fulfill a request that appears to be valid.