logo资料库

ASP.NET 4.0Step.by.Step.pdf

第1页 / 共629页
第2页 / 共629页
第3页 / 共629页
第4页 / 共629页
第5页 / 共629页
第6页 / 共629页
第7页 / 共629页
第8页 / 共629页
资料共629页,剩余部分请下载后查看
Cover
Copyright page
Acknowledgments
Contents at a Glance
Table of Contents
Introduction
Who This Book Is For
Getting Started
Finding Your Best Starting Point in This Book
Conventions and Features in This Book
Conventions
Other Features
Prerelease Software
Hardware and Software Requirements
Code Samples
Digital Content for Digital Book Readers
Installing the C# Code Samples
Using the Code Samples
Uninstalling the Code Samples
Support for This Book
We Want to Hear from You
Part I: Fundamentals
Chapter 1: Web Application Basics
HTTP Requests
HTTP Requests from a Browser
Making HTTP Requests Without a Browser
Hypertext Markup Language
Dynamic Content
HTML Forms
Common Gateway Interface: Very Retro
The Microsoft Environment as a Web Server
Internet Information Services
Internet Services Application Programming Interface DLLs
Running Internet Information Services
Classic ASP: Putting ASP.NET into Perspective
Web Development Concepts
ASP.NET
Chapter 1 Quick Reference
Chapter 2: ASP.NET Application Fundamentals
The Canonical Hello World Application
Mixing HTML with Executable Code
Server-Side Executable Blocks
The ASP.NET Compilation Model
Coding Options
ASP.NET 1.x Style
Modern ASP.NET Style
The ASP.NET HTTP Pipeline
The IIS 5.x and IIS 6.x Pipeline
The IIS 7.x Integrated Pipeline
Tapping the Pipeline
Visual Studio and ASP.NET
Local IIS Web Sites
File System–Based Web Sites
FTP Web Sites
Remote Web Sites
Hello World and Visual Studio
Chapter 2 Quick Reference
Chapter 3: The Page Rendering Model
Rendering Controls as Tags
Packaging the UI as Components
The Page Using ASP.NET
The Page’s Rendering Model
The Page’s Control Tree
Adding Controls Using Visual Studio
Layout Considerations
Chapter 3 Quick Reference
Chapter 4: Custom Rendered Controls
The Control Class
Visual Studio and Custom Controls
A Palindrome Checker
Controls and Events
HtmlTextWriter and Controls
Controls and ViewState
Chapter 4 Quick Reference
Chapter 5: Composite Controls
Composite Controls versus Rendered Controls
Custom Composite Controls
User Controls
When to Use Each Type of Control
Chapter 5 Quick Reference
Chapter 6: Control Potpourri
Validation
How Page Validation Works
Other Validators
Validator Properties
Image-Based Controls
TreeView
MultiView
Chapter 6 Quick Reference
Part II: Advanced Features
Chapter 7: A Consistent Look and Feel
Managing User Interface Consistency
ASP.NET Master Pages
Themes
Skins
Chapter 7 Quick Reference
Chapter 8: Configuration
Windows Configuration
.NET Configuration
Machine.Config
Configuration Section Handlers
Web.Config
Managing Configuration in ASP.NET 1.x
Managing Configuration in Later Versions of ASP.NET
Configuring ASP.NET from IIS
Chapter 8 Quick Reference
Chapter 9: Logging In
Web-Based Security
Securing IIS
Basic Forms Authentication
ASP.NET Authentication Services
The FormsAuthentication Class
An Optional Login Page
Managing Users
ASP.NET Login Controls
Authorizing Users
Chapter 9 Quick Reference
Chapter 10: Data Binding
Representing Collections Without Data Binding
Representing Collections with Data Binding
ListControl-Based Controls
TreeView Control
Menu Control
FormView Control
GridView Control
DetailsView Control
DataList Control
Repeater Control
Simple Data Binding
Accessing Databases
The .NET Database Story
Connections
Commands
Managing Results
ASP.NET Data Sources
Other Data-Bound Controls
LINQ
Chapter 10 Quick Reference
Chapter 11: Web Site Navigation
ASP.NET Navigation Support
Navigation Controls
XML Site Maps
The SiteMapProvider
The SiteMap Class
The SiteMapNode
Using Navigation Controls
The Menu and TreeView Controls
The SiteMapPath Control
Site Map Configuration
Building Navigable Web Sites
Trapping the SiteMapResolve Event
Defining Custom Attributes for Each Node
Security Trimming
URL Mapping
URL Rewriting
Chapter 11 Quick Reference
Chapter 12: Personalization
Personalizing Web Visits
Personalization in ASP.NET
User Profiles
Personalization Providers
Using Personalization
Defining Profiles in Web.Config
Using Profile Information
Saving Profile Changes
Profiles and Users
Chapter 12 Quick Reference
Chapter 13: Web Parts
A Brief History of Web Parts
What Good Are Web Parts?
Developing Web Parts Controls
Web Parts Page Development
Web Parts Application Development
The Web Parts Architecture
WebPartManager and WebZones
Built-In Zones
Built-In Web Parts
Developing a Web Part
Chapter 13 Quick Reference
Part III: Caching and State Management
Chapter 14: Session State
Why Session State?
ASP.NET and Session State
Introduction to Session State
Session State and More Complex Data
Configuring Session State
Turning Off Session State
Storing Session State InProc
Storing Session State in a State Server
Storing Session State in a Database
Tracking Session State
Tracking Session State with Cookies
Tracking Session State with the URL
Using AutoDetect
Applying Device Profiles
Session State Timeouts
Other Session Configuration Settings
The Wizard Control: An Alternative to Session State
Chapter 14 Quick Reference
Chapter 15: Application Data Caching
Getting Started with Caching
Using the Data Cache
Impact of Caching
Managing the Cache
DataSets in Memory
Cache Expirations
Cache Dependencies
The SQL Server Dependency
Clearing the Cache
Chapter 15 Quick Reference
Chapter 16: Caching Output
Caching Page Content
Managing Cached Content
Modifying the OutputCache Directive
The HttpCachePolicy
Caching Locations
Output Cache Dependencies
Caching Profiles
Caching User Controls
When Output Caching Makes Sense
Other Cache Providers
Chapter 16 Quick Reference
Part IV: Diagnostics and Plumbing
Chapter 17: Diagnostics and Debugging
Page Tracing
Tracing
Trace Statements
Application Tracing
Enabling Tracing Programmatically
The TraceFinished Event
Piping Other Trace Messages
Debugging with Visual Studio
Error Pages
Unhandled Exceptions
Chapter 17 Quick Reference
Chapter 18: The HttpApplication Class and HTTP Modules
The Application: A Rendezvous Point
Overriding HttpApplication
HttpModules
Global.asax vs. HttpModules
Chapter 18 Quick Reference
Chapter 19: HTTP Handlers
ASP.NET Request Handlers
The Built-in Handlers
Handlers and IHttpHandler
Handlers and Session State
Generic Handlers (ASHX Files)
Chapter 19 Quick Reference
Part V: Dynamic Data, XBAP, MVC, AJAX, and Silverlight
Chapter 20: Dynamic Data
Dynamic Data Controls
Dynamic Data Details
Chapter 20 Quick Reference
Chapter 21: ASP.NET and WPF Content
Improving Perceived Performance by Reducing ­ Round-Trips
What Is WPF?
How Does WPF Relate to the Web?
Loose XAML Files
XBAP Applications
WPF Content and Web Applications
What About Silverlight?
Chapter 21 Quick Reference
Chapter 22: The ASP.NET MVC Framework
The Model-View-Controller (MVC) Architecture
ASP.NET and MVC
ASP.NET MVC vs. Web Forms
MVC and Testing
How MVC Plays with ASP.NET
Following the Request Path
Chapter 22 Quick Reference
Chapter 23: AJAX
Rich Internet Applications
What Is AJAX?
ASP.NET and AJAX
Reasons to Use AJAX
Real-World AJAX
AJAX in Perspective
ASP.NET Server-Side Support for AJAX
ScriptManager Control
ScriptManagerProxy Control
UpdatePanel Control
UpdateProgress Control
Timer Control
AJAX Client Support
ASP.NET AJAX Control Toolkit
AJAX Control Toolkit Potpourri
Getting Familiar with AJAX
The Timer
Updating Progress
Extender Controls
The AutoComplete Extender
A Modal Pop-up Dialog-Style Component
Chapter 23 Quick Reference
Chapter 24: Silverlight and ASP.NET
Web Applications Mature
What Is Silverlight?
Creating a Silverlight Application
Architecture
XAML
Constructing the Visual Tree
XAML and Namespaces
Compiling the Silverlight Application
Adding Silverlight Content to a Web Page
Using the Object Tag
Using the ASP.NET Silverlight Server-Side Control
Using the JavaScript Function
Controls and Events
Routed Events
Silverlight Controls and Class Members
Silverlight and Layout
Integrating with HTML
Animations
WCF Services and Silverlight
Chapter 24 Quick Reference
Part VI: Services and Deployment
Chapter 25: Windows Communication Foundation
Distributed Computing Redux
A Fragmented Communications API
WCF for Connected Systems
WCF Constituent Elements
Endpoints
Channels
Behaviors
Messages
How WCF Plays with ASP.NET
Side-by-Side Mode
ASP.NET Compatibility Mode
Writing a WCF Service
Building a WCF Client
Chapter 25 Quick Reference
Chapter 26: Deployment
Visual Studio Web Sites
HTTP Web Sites
FTP Web Sites
File System Web Sites
Precompiling
Precompiling for Performance
Precompiling for Deployment
Visual Studio 2010 Deployment Support
Chapter 26 Quick Reference
Index
About the Author
Resources
Survey page
Microsoft® ASP.NET 4 Step by Step George Shepherd
PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2010 by George Shepherd 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: 2010925074 Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 WCT 5 4 3 2 1 0 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 mspinput@microsoft.com. Microsoft, Microsoft Press, Access, ActiveX, DirectX, Expression, Expression Blend, Hotmail, IntelliSense, Internet Explorer, MS, MSDN, MS-DOS, MSN, SharePoint, Silverlight, SQL Server, Visual Basic, Visual C#, Visual Studio, Win32, Windows, Windows Live, Windows NT, Windows Server and Windows Vista are either registered trademarks or trademarks of the Microsoft group of companies. 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: Ben Ryan Developmental Editor: Maria Gargiulo Project Editor: Melissa von Tschudi-Sutton and Maria Gargiulo Editorial Production: Waypoint Press, www.waypointpress.com Technical Reviewer: Kenn Scribner; Technical Review services provided by Content Master, a member of Cover: Tom Draper Design CM Group, Ltd. Body Part No. X16-61997
Dedicated to Sally Bronson Harrison and Gene Harrison, my second mom and dad.
Contents at a Glance Part I . Fundamentals . . . . . . 1 Web Application Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 ASP.NET Application Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . 25 3 The Page Rendering Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4 Custom Rendered Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5 Composite Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 6 Control Potpourri . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Part II . Advanced .Features 7 A Consistent Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 . 8 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 . . 9 Logging In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 . 10 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 . 11 Web Site Navigation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 . 12 Personalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 . 13 Web Parts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Part III . Caching .and .State .Management . 14 Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 . 15 Application Data Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 . 16 Caching Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Part IV .Diagnostics .and .Plumbing . 17 Diagnostics and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 . 18 The HttpApplication Class and HTTP Modules . . . . . . . . . . . . . . 385 . 19 HTTP Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 . . v
vi Contents at a Glance Part V . Dynamic .Data, .XBAP, .MVC, .AJAX, .and .Silverlight . 20 Dynamic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 . 21 ASP.NET and WPF Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 . 22 The ASP.NET MVC Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 . 23 AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 . 24 Silverlight and ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 Part VI .Services .and .Deployment . 25 Windows Communication Foundation . . . . . . . . . . . . . . . . . . . . 555 . 26 Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Table of Contents Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix Part I . Fundamentals . . 1 Web Application Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 HTTP Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 HTTP Requests from a Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Making HTTP Requests Without a Browser . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Hypertext Markup Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Dynamic Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Common Gateway Interface: Very Retro . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 The Microsoft Environment as a Web Server . . . . . . . . . . . . . . . . . . . . . . . 12 Internet Information Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Internet Services Application Programming Interface DLLs . . . . . . . . . . . 13 Running Internet Information Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Classic ASP: Putting ASP .NET into Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Web Development Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ASP .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Chapter 1 Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2 ASP.NET Application Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . 25 The Canonical Hello World Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Mixing HTML with Executable Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Server-Side Executable Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 The ASP .NET Compilation Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Coding Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 ASP .NET 1 .x Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Modern ASP .NET Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 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/ . . vii
viii Table of Contents The ASP .NET HTTP Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 The IIS 5 .x and IIS 6 .x Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 The IIS 7 .x Integrated Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Tapping the Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Visual Studio and ASP .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Local IIS Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 File System–Based Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 FTP Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Remote Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Hello World and Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Chapter 2 Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 . . . 3 The Page Rendering Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Rendering Controls as Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Packaging the UI as Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 The Page Using ASP .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 The Page’s Rendering Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 The Page’s Control Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Adding Controls Using Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Layout Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Chapter 3 Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 4 Custom Rendered Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The Control Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Visual Studio and Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 A Palindrome Checker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Controls and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 HtmlTextWriter and Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Controls and ViewState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Chapter 4 Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 5 Composite Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Composite Controls versus Rendered Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Custom Composite Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 When to Use Each Type of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Chapter 5 Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
分享到:
收藏