1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft .NET Framework 3.5 - ASP.NET Application Development

1,1K 499 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Microsoft .NET Framework 3.5 - ASP.NET Application Development
Tác giả Glenn Johnson, GrandMasters, Microsoft Corporation
Trường học Microsoft Corporation
Chuyên ngành ASP.NET Application Development
Thể loại étude de cas
Năm xuất bản 2009
Thành phố Redmond
Định dạng
Số trang 1.072
Dung lượng 11,68 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Training Kit Asp.Net của Microsoft

Trang 2

Exam 70-562: TS: Microsoft NET Framework 3.5, ASP.NET Application Development

cOnfiguring and depLOying Web appLicatiOns (10 percent)

Configure authentication, authorization, and impersonation 14 3

Configure projects, solutions, and reference assemblies 1 3

Configure session state by using Microsoft SQL Server, State Server, or InProc 4 2

Compile an application by using Visual Studio or command-line tools 16 1,2

cOnsuming and creating server cOntrOLs (20 percent)

Implement client-side validation and server-side validation 3 1

WOrking With data and services (17 percent)

Manipulate data by using DataSet and DataReader objects 7 1,2

Call a Windows Communication Foundation (WCF) service or a Web service from an

trOubLeshOOting and debugging Web appLicatiOns (16 percent)

WOrking With asp.net ajaX and cLient-side scripting (15 percent)

targeting mObiLe devices (5 percent)

Trang 3

Add mobile Web controls to a Web page 15 1

prOgramming Web appLicatiOns (17 percent)

Implement session state, view state, control state, cookies, cache, or application state 2,4 Chapter 2:

Lesson 1 Chapter 4: Lesson 1

Lessons 1 and 3; Chapter 3: Lessons 1 and 2

exam Objectives The exam objectives listed here are current as of this book’s publication date Exam objectives

are subject to change at any time without prior notice and at Microsoft’s sole discretion Please visit the Microsoft Learning Web site for the most current listing of exam objectives: http://www.microsoft.com/learning/en/us/exams /70-562.mspx.

Trang 4

PUBLISHED BY

Microsoft Press

A Division of Microsoft Corporation

One Microsoft Way

Redmond, Washington 98052-6399

Copyright © 2009 by Glenn Johnson, GrandMasters, and Microsoft Corporation

All rights reserved No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher

Library of Congress Control Number: 2009920804

Printed and bound in the United States of America

1 2 3 4 5 6 7 8 9 QWT 4 3 2 1 0 9

Distributed in Canada by H.B Fenn and Company Ltd

A CIP catalogue record for this book is available from the British Library

Microsoft Press books are available through booksellers and distributors worldwide For further infor-mation about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at

fax (425) 936-7329 Visit our Web site at www.microsoft.com/mspress Send comments to tkinput@microsoft.com.

Microsoft, Microsoft Press, Active Directory, ActiveX, Excel, IntelliSense, Internet Explorer, MS, MSDN, SharePoint, Silverlight, SQL Server, Visual Basic, Visual SourceSafe, Visual Studio, Win32, Windows, Windows Mobile, Windows

NT, Windows Server, and Windows Vista are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries Openwave and the Openwave logo are trademarks of Openwave Systems Inc All rights reserved Other product and company names mentioned herein may be the trademarks of their respective owners.The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred

This book expresses the author’s views and opinions The information contained in this book is provided without any express, statutory, or implied warranties Neither the authors, Microsoft Corporation, nor its resellers, or distributors will

be held liable for any damages caused or alleged to be caused either directly or indirectly by this book

Acquisitions Editor: Ken Jones

Developmental Editor: Laura Sackerman

Project Editor: Maureen Zimmerman

Editorial Production: nSight, Inc.

Technical Reviewer: Kurt Meyer; Technical Review services provided by Content Master, a member of CM Group, Ltd Cover: Tom Draper Design

Body Part No X15-45849

Trang 5

Many thanks to Chris Norton and his team at nSight (www.nsightworks.com) for keeping

the book on schedule This includes Teresa Horton for her great editing work

Thanks also to Richard Kobylka and the team at GrandMasters (www.grandmasters.biz) for

their help shepherding this book through the process

Also, thanks go to my technical editor, Kurt Meyer Thanks for working through the labs, getting the CD right, and challenging various technical anomalies

Thanks to Kristy Saunders for her wonderful work on the sample questions for the CD.Finally, I would like to thank my wife, Carrie, and my children, Allie and Ben Thanks for your patience and understanding during this process

—Mike Snell

Trang 7

What do you think of this book? We want to hear from you!

Microsoft is interested in hearing your feedback so we can continually improve our

books and learning resources for you To participate in a brief online survey, please visit:

Contents

System Requirements xxi

Software Requirements xxii

Using the CD and DVD xxii

Microsoft Certified Professional Program xxvi

Technical Support xxvi

Evaluation Edition Software Support xxvii

Before You Begin 2

Lesson 1: Understanding the Players 3

Understanding the Role of Hypertext Transfer Protocol 6

Trang 8

Lesson 2: Creating a Web Site and Adding New Web Pages 16

Lesson 3: Working with Web Configuration Files 33

Understanding the Configuration File Hierarchy 33

Create a New Web Site Using Visual Studio 2008 40

Take a Practice Test 41

Before You Begin 44 Lesson 1: Understanding and Using Server Controls 45

Understanding the Life Cycle of an ASP.NET Web Page

Trang 9

Lesson Review 71

Lesson 2: Exploring Common Server Controls 73

Lesson 3: Exploring Specialized Server Controls 84

The Table, TableRow, and TableCell Controls 87

Create a New Web Page Using Server Controls 140

Create Event Handlers for Pages and Controls 140

Take a Practice Test 140

Trang 10

chapter 3 input validation and site navigation 141

Before You Begin 142 Lesson 1: Performing Input Validation 143

Understanding the BaseCompareValidator Class 147

Understanding the RequiredFieldValidator Control 148

Using the RegularExpressionValidator Control 151

Lesson 2: Performing Site Navigation 165

Create a Data Collection Page with Validation 181

Implement the HyperLink Web Server Control 181 Take a Practice Test 182

Trang 11

chapter 4 asp.net state management 183

Before You Begin 184

Lesson 1: Using Client-Side State Management 185

Choosing Client-Side or Server-Side State Management 186

Maintain State by Using Database Technology 227

Take a Practice Test 228

chapter 5 customizing and personalizing a Web application 229

Before You Begin 230

Lesson 1: Using Master Pages 231

Trang 12

Overview of Master and Content Pages 232

Referencing Master Page Properties and Controls

Handling Events When Working with Master Pages 239

Adding a Cascading Style Sheet to Your Theme 253

Lesson 3: Using Web Parts 263

Enabling Users to Arrange and Edit Web Parts 269

Trang 13

Suggested Practices 294

Implement a Consistent Page Design by Using Master Pages 294

Customize a Web Page by Using Themes and User Profiles 295

Take a Practice Test 295

chapter 6 Working with asp.net ajaX

Before You Begin 298

Lesson 1: Creating AJAX-Enabled Web Forms 299

Lesson 2: Creating Client Scripts with the AJAX Client-Side Library 316

Create Client Code Using the Microsoft AJAX Library 370 Take a Practice Test 370

Before You Begin 372

Lesson 1: Using the ADO.NET Disconnected Classes 373

Trang 14

Getting Started with the DataTable Object 373

Lesson 2: Using the ADO.NET Connected Classes 421

Create a Web Page for Updating Database Data 532

Create a Web Page for Editing Disconnected Data 532

Create a Web Page for Editing Connected Data 533

Create a Web Page for Working with XML Data 533

Create a Web Page for Reading, Modifying,

Take a Practice Test 534

chapter 8 Working with data source and

Before You Begin 535

Trang 15

Lesson 1: Connecting to Data with Data Source Controls 537

Using Objects as Data Sources with ObjectDataSource 539

Connecting to Relational Databases with SqlDataSource 548

Working with Microsoft Access Data Files

Connecting to XML Data Using XmlDataSource 553

Connecting to LINQ-Based Data with LinqDataSource 558

Connecting to Site Navigation Data with SiteMapDataSource 559

Lesson 2: Working with Data-Bound Web Server Controls 569

Create a Master-Detail Solution Using the

Take a Practice Test 604

Before You Begin 606

Lesson 1: Creating and Consuming XML Web Services 607

Trang 16

Lesson Summary 625

Lesson 2: Creating and Consuming WCF Services 628

Presenting Windows Communication Foundation (WCF) 628

Consuming a WCF Service in an ASP.NET Page 635

Calling a WCF Service from Client Script Using

Take a Practice Test 656

Before You Begin 658 Lesson 1: Working with User Controls 659

Lesson 2: Working with Custom Web Server Controls 683

Adding Toolbox Support for a Custom Web Server Control 689

Creating a Custom Designer for a Custom Control 693

Trang 17

Creating a Composite Control 695

Take a Practice Test 709

Before You Begin 712

Lesson 1: Using Web Site Programmability 713

Lesson 2: Using the ASP.NET Intrinsic Objects 730

Trang 18

Using Web Site Programmability 745

Take a Practice Test 746chapter 12 monitoring, troubleshooting, and debugging 747

Before You Begin 748 Lesson 1: Debugging an ASP.NET Application 749

Take a Practice Test 780

Before You Begin 781 Lesson 1: Configuring Globalization and Localization 783

Trang 19

HTML Layout Best Practices 792

Lesson 2: Configuring Accessibility 800

How ASP.NET Controls Support Accessibility 801

Improving the Accessibility of Forms Requiring User Input 803

Take a Practice Test 813

chapter 14 implementing user profiles, authentication,

Before You Begin 816

Lesson 1: Working with User Profiles 817

Lesson 2: Using ASP.NET Membership 833

Trang 20

Using WSAT to Configure Security 834

Lesson 3: Securing Your Site 849

Configuring Web Applications to Require

Configuring Web Applications for Anonymous Access Only 859

Configuring Impersonation by Using config Files 860

Restricting Access to ASP.NET Web Applications,

Establish a User’s Identity by Using Forms Authentication 871

Use Authorization to Establish the Rights of an

Before You Begin 876 Lesson 1: Building Mobile Applications 877

Trang 21

The ASP.NET Mobile Web Application Roadmap 877

Viewing and Testing Mobile Web Applications 880

Applying Best Practices to Mobile Applications 895

Create a Data-Collection Page with Validation 910 Take a Practice Test 911

chapter 16 deploying, configuring, and caching applications 913

Before You Begin 914

Lesson 1: Deploying Web Applications 915

Writing to the Registry as Part of Deployment 924

Adding Custom Actions to Your Deployment 928

Deploying Web Applications Using a Web Setup Project 929

Deploying Web Applications Using the Copy Web Tool 934

Trang 22

Lesson Summary 941

Lesson 2: Using Caching to Improve Performance 943

Programmatically Invalidating Cached Pages 952

Optimize and Troubleshoot a Web Application 962 Take a Practice Test 962

What do you think of this book? We want to hear from you!

Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you To participate in a brief online survey, please visit:

www.microsoft.com/learning/booksurvey/

Trang 23

This training kit is designed for developers who plan to take Microsoft Certified

Techni-cal Specialist (MCTS) exam 70-562, as well as for developers who need to know how to

develop applications using the Microsoft NET Framework 3.5 and ASP.NET 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:

n Create a Web application using Web server controls, event handlers, application state,

and session state

n Create custom Web server controls

n Develop accessible Web applications that can be used by a global audience

n Write rich, Web-based user experiences using AJAX

n Integrate a Web application with a back-end database

n Create a Web application that stores user-specific information and preferences

n Monitor, troubleshoot, and debug ASP.NET applications

n Build service-oriented applications (SOAs) using Web services and Windows

Communi-cation Foundation (WCF)

n Add authentication and authorization features to your application to improve security

and add multiple access levels

n Create Web applications that can be used from mobile phones and PDAs

system requirements

We recommend that you use a computer that is not your primary workstation to do the

practice exercises in this book because you will make changes to the operating system and

application configuration

Hardware Requirements

To use the companion CD, you need a computer running Microsoft Windows Server 2003,

Windows Server 2008, Windows Vista, or Windows XP The computer must meet the

follow-ing minimum requirements:

Trang 24

n 1 GHz 32-bit (x86) or 64-bit (x64) processor (depending on the minimum requirements

of the operating system)

n 1 GB of system memory (depending on the minimum requirements of the operating system)

n A hard disk with at least 700 MB of available space

n A monitor capable of at least 800 × 600 display resolution

n A keyboard

n A mouse or other pointing device

n An optical drive capable of reading CD-ROMs

n An optical drive capable of reading DVDs if you are installing the 90-day evaluation edition of Visual Studio 2008 Professional Edition included on the DVD with this book

software requirements

The computer used with the companion CD-ROM should also have the following software:

n A Web browser such as Microsoft Internet Explorer version 6 or later

n An application that can display PDF files, such as Adobe Acrobat Reader, which can be

downloaded at www.adobe.com/reader

n Visual Studio 2008 (A 90-day evaluation edition of Visual Studio 2008 Professional tion is included on the DVD with this book.)

Edi-These requirements will support use of the companion CD-ROM

using the cd and dvd

A companion CD and an evaluation software DVD are included with this training kit The evaluation software DVD contains a 90-day evaluation edition of Microsoft Visual Studio 2008 Professional Edition, in case you want to use it with this book The companion CD contains the following:

PRACTICE TESTS

You can reinforce your understanding of how to create ASP.NET applications using the NET Framework 3.5 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-562 certification exam by using tests created from a pool of 200 realistic exam questions, which is enough to give you many different practice exams to ensure that you’re prepared

Trang 25

The chapters in this book include sample fi les 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 while 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 Document Format (PDF); you can view it

by using Adobe Acrobat or Adobe Reader You can use the eBook to cut and paste code as

you work through the exercises

gLoSSARy

Key terms used in the chapters are included in a glossary on the CD

SAMPLE CHAPTERS

The CD also includes sample chapters from other MS Press books for your review

MORE INFO digitaL cOntent fOr digitaL bOOk readers

If you bought a digital-only edition of this book, you can enjoy select content from the

print edition’s companion CD Visit http://go.microsoft.com/fwlink/?LinkId=144372 to get

your downloadable content This content is always up-to-date and available to all readers

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 fi le on the CD-RoM for alternate installation

instructions

2. Click the Practice Tests item, and follow the instructions on the screen

MORE INFO digitaL cOntent fOr digitaL bOOk readers

If you bought a digital-only edition of this book, you can enjoy select content from the

print edition’s companion CD Visit http://go.microsoft.com/fwlink/?LinkId=144372 to get

your downloadable content This content is always up-to-date and available to all readers.

NOTE if the cd menu dOesn’t appear

NOTE if the cd menu dOesn’t appear

NOTE

If the CD menu or the license agreement doesn’t appear, AutoRun might be disabled on

your computer Refer to the Readme.txt fi le on the CD-RoM for alternate installation

instructions.

Trang 26

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-562) Microsoft NET Framework 3.5—Web-Based Client Development lesson review to use the questions from the “Lesson Review” sections of this book Select the (70- 562) Microsoft NET Framework 3.5 — Web-Based Client Development practice test to use a

pool of 200 questions similar to those in the 70-562 certifi cation exam

Lesson Review options

When you start a lesson review, the Custom Mode dialog box appears so that you can

con-fi gure 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

After you click OK, your lesson review starts

n To take the test, answer the questions and use the Next, Previous, and Go To buttons

to move from question to question

n After you answer an individual question, if you want to see which answers are rect—along with an explanation of each correct answer—click Explanation

cor-n If you’d rather wait until the end of the test to see how you did, answer all the tions, 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 objective You can print

ques-a copy of your test, review your ques-answers, or retques-ake the test

Practice Test options

When you start a practice test, you choose whether to take the test in Certifi cation Mode, Study Mode, or Custom Mode:

NOTE LessOn revieWs vs practice tests

NOTE LessOn revieWs vs practice tests

NOTE

Select the (70-562) Microsoft NET Framework 3.5—Web-Based Client Development lesson

review to use the questions from the “Lesson Review” sections of this book Select the review

(70-562) Microsoft NET Framework 3.5 — Web-Based Client Development practice test to use a practice test to use a practice test

pool of 200 questions similar to those in the 70-562 certifi cation exam.

Trang 27

CERTIFICATIoN MoDE

Closely resembles the experience of taking a certifi cation 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 customize them as you like

In all modes, the user interface you see when taking the test is 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”

sec-tion is provided that lists where in the training kit you can fi nd the informasec-tion 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 Learning Plan tab to see a list of references

for every objective

How to Uninstall the Practice Tests

To uninstall the practice test software for a training kit, use the Add Or Remove Programs

option in Windows Control Panel

How to Install the Code

To install the sample fi les 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 fi le 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

\MicrosoftPress\TK562

NOTE if the cd menu dOesn’t appear

NOTE if the cd menu dOesn’t appear

NOTE

If the CD menu or the license agreement doesn’t appear, AutoRun might be disabled on

your computer Refer to the Readme.txt fi le on the CD-RoM for alternate installation

instructions.

Trang 28

microsoft certifi ed professional program

The Microsoft certifi cations provide the best method to prove your command of

cur-rent Microsoft products and technologies The exams and corresponding certifi cations are developed to validate your mastery of critical competencies as you design and develop, or implement and support, solutions with Microsoft products and technologies Computer professionals who become Microsoft-certifi ed are recognized as experts and are sought after industry-wide Certifi cation brings a variety of benefi ts to the individual and to employers and organizations

MORE INFO aLL the micrOsOft certificatiOns

For a full list of Microsoft certifi cations, go to www.microsoft.com/learning/mcp/default.asp

• Microsoft Press Attn: MCTS Self-Paced Training Kit (Exam 70-562): Microsoft NET Framework 3.5— Web-Based Client Development Editor

One Microsoft Way Redmond, WA 98052–6399

For additional support information regarding this book and the CD-ROM (including answers to commonly asked questions about installation and use), visit the Microsoft Press

Technical Support Web site at 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 regarding Microsoft software, please connect to http:// support.microsoft.com

MORE INFO aLL the micrOsOft certificatiOns

For a full list of Microsoft certifi cations, go to www.microsoft.com/learning/mcp/default.asp.

Trang 29

evaluation edition software support

The 90-day evaluation edition provided with this training kit is not the full retail product and

is provided only for the purposes of training and evaluation Microsoft and Microsoft

Techni-cal 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 (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 www.microsoft.com.

Trang 31

c h a p t e r 1

Introducing ASP.NET 3.5

The Web development experience continues to evolve with Microsoft Visual Studio 2008 and ASP.NET 3.5 These new tools enable you to build highly interactive, robust Web applications more efficiently than ever This includes building sites that provide users with

a high degree of interactivity with AJAX There are also new and improved controls that both speed development and add to the user experience Other enhancements include improvements in Web site security, integration with Microsoft Internet Information Services 7.0 (IIS 7.0), and a better Web service programming model All of these enhancements were developed to give ASP.NET developers more control and increased confidence when build-ing and deploying the next generation of Web sites

This chapter introduces the basics of Web site development with ASP.NET It starts by describing the key players in any Web site: the server, the browser (or client), and Hypertext Transfer Protocol (HTTP) This serves as a basis for understanding the architecture of an ASP.NET Web site In Lesson 2, “Creating a Web Site and Adding New Web Pages,” you will learn the key components that make up an ASP.NET development site The chapter closes with a discussion on configuring the many aspects of an ASP.NET application

Exam objectives in this chapter:

n Configuring and Deploying Web Applications

n Configure projects, solutions, and reference assemblies

n Programming Web Applications

n Implement business objects and utility classes

n Handle events and control page flow

Lessons in this chapter:

n Understanding the Players 3

n Creating a Web Site and Adding New Web Pages 16

n Working with Web Configuration Files 33

Trang 32

before you begin

To complete the lessons in the chapter, you should be familiar with developing applications in Microsoft Visual Studio using Visual Basic or C# In addition, you should be comfortable with all of the following:

n The Visual Studio 2008 Integrated Development Environment (IDE)

n A basic understanding of Hypertext Markup Language (HTML) and client-side scripting

n How to make assemblies available to other applications

n Working with Visual Studio projects, fi les, classes, and designers

to understand Extensible Markup Language (XML), Web services, and database programming of course, you also need to know how all of these things work together to form a single solution The modern Web developer needs to know more technologies (and be able to easily switch between them) than any other developer

in history I think this is one of the reasons Web development is such a challenging, fun, and rewarding experience

to understand Extensible Markup Language (XML), Web services, and database programming of course, you also need to know how all of these things work together to form a single solution The modern Web developer needs to know more technologies (and be able to easily switch between them) than any other developer

in history I think this is one of the reasons Web development is such a challenging, fun, and rewarding experience

Trang 33

Lesson 1: understanding the players

A Web application is unlike a standard Windows application It does not run in a single

process on a single machine Instead, it is typically hosted on a Web server and accessed via

a Web browser on a client machine The communication between the Web server and Web

browser is sent using HTTP It is imperative you have a basic understanding of how these

items work and communicate together before you start writing much code The typical

com-munication process between browser and server can be generalized into the following steps:

1. A user uses his or her Web browser to initiate a request for a Web server resource

2. HTTP is used to send a GET request to the Web server.

3. The Web server processes the GET request on the server (typically locating the

requested code and running it)

4. The Web server then sends a response back to the Web browser The HTTP protocol is

used to send the HTTP response back to the Web browser

5. The user’s Web browser then processes the response (typically HTML and JavaScript)

and renders the Web page for display to the user

6. The user may then enter data and perform an action such as clicking a submit button

that causes his or her data to be sent back to the Web server for processing

7. HTTP is used to POST the data back to the Web server.

8. The Web server then processes the POST request (again, calling your code in the

process)

9. The Web server then sends a response back to the Web browser HTTP is used to send

the HTTP response to the Web browser

10. The Web browser again processes the response and displays the Web page to the user

This process is repeated over and over during a typical Web application session

This lesson provides an overview of the responsibilities and boundaries of a Web browser

and the Web server You will also learn the basics of HTTP and how browsers and servers use

it to process user requests

Trang 34

After this lesson, you will be able to:

n Describe the Web server’s role in responding to requests for resources

n Describe the Web browser’s role in submitting requests and presenting the response to the user

n Describe the role of HTTP in communicating with the Web server

n Describe how HTTP verbs are used to request resources from the Web server

n Describe the status-code groups that are implemented in HTTP

n Describe Distributed Authoring and Versioning

n Describe PostBack, the common method of sending data to the Web server

n Describe methods for troubleshooting HTTP

Estimated lesson time: 30 minutes

The Web Server’s Role

The fi rst Web servers were responsible for receiving and processing simple user requests from browsers via HTTP The Web server handled its request and sent a response back to the Web browser The Web server then closed any connection between it and the browser and released all resources that were involved with the request These resources were easy to release as the Web server was fi nished processing the request This type of Web application

was considered to be stateless because no data was held by the Web server between requests

and no connection was left open These applications typically involved simple HTML pages and were therefore able to handle thousands of similar requests per minute Figure 1-1 shows

an example of this simple, stateless environment

Browser initiates communication with page request

Server responds withWeb page (HTML)

HTTP GET(default.html)

Display page

Process GET request

Send response andclose server connection

figure 1-1 A simple request and response between browser and server in a stateless environment

After this lesson, you will be able to:

n Describe the Web server’s role in responding to requests for resources

n Describe the Web browser’s role in submitting requests and presenting the response to the user

n Describe the role of HTTP in communicating with the Web server

n Describe how HTTP verbs are used to request resources from the Web server

n Describe the status-code groups that are implemented in HTTP

n Describe Distributed Authoring and Versioning

n Describe PostBack, the common method of sending data to the Web server PostBack, the common method of sending data to the Web server PostBack

n Describe methods for troubleshooting HTTP

Estimated lesson time: 30 minutes

Trang 35

Today’s Web servers deliver services that go far beyond the original Web servers In

addi-tion to serving static HTML files, modern Web servers also handle requests for pages that

contain code that executes on the server; the Web server executes this code on request and

responds with the results These Web servers also have the ability to store data between

requests This means that Web pages can be connected together to form a Web

applica-tion that understands the current state of each individual user’s requests These servers keep

a connection open to the browser for a period of time in anticipation of additional page

requests from the same user This type of interaction is illustrated in Figure 1-2

Browser initiates communication with page requestHTTP GET

(default.html) Process GET request

Server responds withWeb page (HTML)Display page Send response

Execute server-side code

Store session data,e.g., OrderId=1234

figure 1-2 Modern Web servers store state between page requests to enable more sophisticated Web

applications

The Web Browser’s Role

The Web browser provides a platform-independent means of displaying Web pages that

were written in HTML HTML was designed to be able to render information on any operating

system while placing no constraint on the window size This is why Web pages are considered

platform independent HTML was designed to “flow,” wrapping text as necessary to fit into

the browser window The Web browser also displays images and responds to hyperlinks to

other pages Each Web page request to the Web server results in the Web browser updating

the screen to display the new information

Although the Web browser’s role is simply to present information and collect data from

users, many new client-side technologies enable today’s Web browsers to execute code such

as JavaScript and to support plug-ins that improve the user’s experience Technologies such

as Asynchronous JavaScript and XML (AJAX) and Microsoft Silverlight allow Web browsers

to communicate with Web servers without clearing the existing Web page from the browser

window These technologies make the user experience more dynamic and interactive

Trang 36

Understanding the Role of Hypertext Transfer Protocol

HTTP is a text-based communication protocol that is used to request Web pages from the Web server and send responses back to the Web browser HTTP messages are typically sent between the Web server and Web browser using port 80, or port 443 when using Secure HTTP (HTTPS)

MORE INFO http/1.1 specificatiOn

For more information on the current HTTP standard (HTTP/1.1), you can review the specifi

-cation at the following Web address: http://www.w3.org/Protocols/rfc2616/rfc2616.html

Pages are typically requested by a user entering information in his or her browser by ing on a favorite, conducting a search, or typing in a Uniform Resource Locator (URL) When

click-a Web pclick-age is requested, click-a textuclick-al commclick-and is sent from the browser to the Web server This command might look as follows:

GET /default.aspx HTTP/1.1

Host: www.northwindtraders.com

The fi rst line in this command contains what is called the method, also known as the verb

or command In this case the verb is GET The verb is followed by the URL of the Web page to

be retrieved (/default.aspx) Following that is the version of HTTP to be used to process the command (HTTP/1.1) In this way, the method indicates the action to be performed by the Web server, the URL that is the target of that method, and the communication protocol

The second line of the command (Host: www.northwindtraders.com) identifi es the name of

the host that should be used by the Web server This is useful if a Web server is hosting more than one Web site In this case, the Web server needs to pass on the request to the appro-priate site for processing This process is known as using host headers to identify the Web site that will handle the given request There are other methods defi ned by HTTP Table 1-1 contains a list of some of the common HTTP methods with a description of their uses Note that if Distributed Authoring and Versioning (DAV) is enabled on the Web site, many more

verbs are available, such as LOCK and UNLOCK

What is distributed authoring and versioning?

Distributed Authoring and Versioning (DAV) is a set of extensions to HTTP/1.1 that simplifi es 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 fi les and the ability to designate versions

DAV is built directly on HTTP/1.1, so no other protocols, such as File Transfer col (FTP) or Server Message Block (SMB), are required DAV also provides the ability

Proto-to query the Web server for various resource properties such as fi le names,

MORE INFO http/1.1 specificatiOn For more information on the current HTTP standard (HTTP/1.1), you can review the specifi -

cation at the following Web address: http://www.w3.org/Protocols/rfc2616/rfc2616.html.

What is distributed authoring and versioning?

Distributed Authoring and Versioning (DAV) is a set of extensions to HTTP/1.1 that simplifi es 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 fi les and the ability to designate versions

DAV is built directly on HTTP/1.1, so no other protocols, such as File Transfer col (FTP) or Server Message Block (SMB), are required DAV also provides the ability

Proto-to query the Web server for various resource properties such as fi le names,

Trang 37

time stamps, and sizes DAV also gives developers the ability to perform server-side

fi le copying and moving For example, you can use the HTTP geT and PUT verbs to

retrieve fi les from the Web servers and save them to different locations, or you can

use the DAV’s CoPy verb to simply tell a server to copy the fi le

tabLe 1-1 Common HTTP/1.1 Methods

http methOd descriptiOn

OPTIONS Used by client applications to request a list of all supported verbs In this

way, you can check to see if a server allows a particular verb before ing network bandwidth trying to send an unsupported request

wast-GET Gets a URL from the server A GET request for a specifi c URL, say, /test.

htm, retrieves the test.htm fi le 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 fi les If you request a directory, the server can be confi gured to return a default fi le, such as index.html, that may be representative of the directory

HEAD Retrieves the meta information for a resource This information is typically

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 Sends data to the Web server for processing This is typically the result of

users entering data on a form and submitting that data as part of their request

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 fi le specifi ed

in the URL, and copies the received data to the newly created fi le If the

fi le exists and is not locked, the content of the fi le will be overwritten

DELETE Used to delete a resource at the Web server Requires write permissions

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

tun-nel, such as Secure Sockets Layer (SSL) protocol

DEBUG Not defi ned in the HTTP/1.1 specifi cation, but used to start ASP.NET

debugging This method informs Visual Studio of the process to which the debugger will attach

time stamps, and sizes DAV also gives developers the ability to perform server-side

fi le copying and moving For example, you can use the HTTP geT and geT and geT PUT verbs to PUT

retrieve fi les from the Web servers and save them to different locations, or you can

use the DAV’s CoPy verb to simply tell a server to copy the fi le CoPy verb to simply tell a server to copy the fi le CoPy

Trang 38

The communication from the Web browser to the Web server is referred to as a request

In ASP.NET, there is a Request object that is used to represent the Web browser’s

communi-cations to the Web server It wraps the resource request in an object that can be queried in code This includes providing your code access to things like the cookies associated with your site, the query string parameters passed on the URL, the path to the request, and more The communication from the Web server back to the Web browser is commonly referred

to as the response In ASP.NET this information is wrapped in the Response object You can

use this object to set cookies, defi ne caching, set page expiration, and more When the Web

server responds to a request, it uses what it fi nds in the Response object to write the actual,

text-based HTTP response This communication might look as follows:

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 to complete the

request

4xx Client Error: The request has a syntax error or the server does not know

how to fulfi ll the request

5xx Server Error: The server failed to fulfi ll a request that appears to be valid

In addition to the status code groups, HTTP/1.1 defi nes unique status codes and reasons

A reason is nothing more than a very brief description of the status code Table 1-3 shows a list of common status codes and reasons Reason text can be modifi ed without breaking the protocol

Trang 39

tabLe 1-3 Common Status Codes and Their Reasons

status cOde reasOn

The second line of the response indicates the type of Web server (e.g., Server:

Microsoft-IIS/6.0) The third line (Content-Type) indicates the type of resource that is being sent to the

Web browser as part of the response This indicator is in the form of a Multipurpose Internet

Mail Extensions (MIME) type In the case of this example (Content-Type: text/html), the file is a

static HTML text file The MIME type is a two-part designator type/subtype, in which the first

part is the resource type (text in this example) and the second part is the resource subtype

(html in this example) Some common MIME types are shown in Table 1-4

tabLe 1-4 Common MIME Types

mime type descriptiOn

Text Textual information No special software is required to get the full

meaning of the text, aside from support for the indicated character set

One subtype is plain, which means that the text can be read without requiring additional software Other subtypes are html and xml, which

indicate the appropriate file type(s)

Image Image data Requires a display device (such as a graphical display or a

graphics printer) to view the information Subtypes are defined for two

widely used image formats, jpeg and gif.

Trang 40

mime type descriptiOn

Audio Audio data Requires an audio output device (such as a speaker or

head-phones) to “hear” the contents An initial subtype called basic is defi ned

for this type

Video Video data Requires the capability to display moving images, typically

including specialized hardware and software An initial subtype called

mpeg is defi ned for this type.

Application Other kinds of data, typically either uninterpreted binary data or

information to be processed by an application The subtype, called

octet-stream, is to be used in the case of uninterpreted binary data, in

which the simplest recommended action is to offer to write the

informa-tion into a fi le for the user The PostScript subtype is also defi ned for the

transport of PostScript material

MORE INFO mime types The Windows registry contains a list of MIME types/subtypes at the following location: HKEy_CLASSES_RooT\MIME\Database\Content Type

The next line is content length (Content-Length: 38 in this example) This simply indicates the size of the content that follows After the content-length line, the response message is returned This message is based on the MIME type The browser attempts to process the content based on its MIME type For example, it interprets HTML for HTML MIME types and renders an image for image MIME types

Submitting Form Data to the Web Server

The HTML <form> tag can be used to create a Web page that collects data from the user and sends the collected data back to the Web server The form tag is nested inside the <HTML>

tags The form tags typically include information for the user in the form of text and input

tags for defi ning things like buttons and text boxes A typical use of the <form> tag might

look like this:

<form method="POST" action="getCustomer.aspx">

Enter Customer ID:

<input type="text" name="Id">

<input type="submit" value="Get Customer">

</form>

This example form prompts the user for a customer ID, displays a text box that collects the desired customer ID, and also displays a Submit button that initiates the sending of data to

the Web server The method attribute of the form tag indicates the HTTP verb (POST) to use

MORE INFO mime types The Windows registry contains a list of MIME types/subtypes at the following location: HKEy_CLASSES_RooT\MIME\Database\Content Type

Ngày đăng: 29/11/2013, 01:00

TỪ KHÓA LIÊN QUAN