Programming Microsoft
®
ASP.NET 2.0 Applications:
Advanced Topics
Dino Esposito
(Solid Quality Learning)
A01T621772.fm Page 1 Friday, February 10, 2006 2:52 PM
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2006 by Dino Esposito
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 2005939240
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 QWT 0 9 8 7 6 5
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 information
about international editions, contact your local Microsoft Corporation office or contact Microsoft Press Inter-
national directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments
to mspinput@microsoft.com.
Active Directory, ActiveX, InfoPath IntelliSense, JScript, Microsoft Press, MSDN, SharePoint, Tahoma, Ver-
dana, Visio, Visual Basic, Visual Studio, Webdings, WebTV, Win32, Windows, Windows CE, Windows NT,
and Windows Server are either registered trademarks or trademarks of Microsoft Corporation in the United
States and/or other countries. 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 with-
out 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
Project Editor: Lynn Finnel
Technical Editor: Kenn Scribner
Copy Editor: Roger LeBlanc
Indexer: Lynn Armstrong
Body Part No. X11-53582
A02L621772.fm Page ii Friday, February 10, 2006 2:52 PM
vii
Table of Contents
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii
Part
I Inside the ASP.NET Machinery
1 The ASP.NET Compilation Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
The ASP.NET Runtime Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The ASP.NET Process Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
ASP.NET Temporary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
The Hidden Code of an ASP.NET Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Application Precompilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
In-Place Precompilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Precompilation for Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Building Blocks of ASP.NET Compilation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Options for Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
ASP.NET Reserved Folders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Build Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Architecting a Custom Build Provider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Virtual Path Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2 HTTP Handlers and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Quick Overview of the IIS Extensibility API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
The ISAPI Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Changes Coming Soon in IIS 7.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Writing HTTP Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
The IHttpHandler Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
An HTTP Handler for Quick Data Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
The Picture Viewer Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Advanced HTTP Handler Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
What do you think of this book?
We want to hear from you!
Microsoft is interested in hearing your feedback about this publication 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/
A05T621772.fm Page vii Friday, February 10, 2006 2:54 PM
viii Table of Contents
Writing HTTP Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The IHttpModule Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
A Custom HTTP Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The Page Refresh Feature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
3 ASP.NET Configuration Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The ASP.NET Configuration Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Configuration Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The <location> Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The <system.web> Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Other Top-Level Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Managing Configuration Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Using the Configuration API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Using Management Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
4 Building Custom ASP.NET Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
The Pattern of Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
The Theory—Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
The Practice—Implementation Details. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Built-In ASP.NET Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
The Membership Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
The Role Management Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
The User’s Profile Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
The Session State Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
The Site Mapping Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
The Web Event Handling Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
The Web Parts Personalization Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
The Configuration Protection Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Roll Your Own Provider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Design Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Implementation of a Cookie-Based Profile Provider . . . . . . . . . . . . . . . . . . . . 180
Putting It All Together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
A05T621772.fm Page viii Friday, February 10, 2006 2:54 PM
Table of Contents ix
5 Building Feature-Rich Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Building Asynchronous Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Tools for Asynchronous ASP.NET Programming . . . . . . . . . . . . . . . . . . . . . . . . 192
ASP.NET 2.0 Facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Implementation of Asynchronous Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Real-World Scenarios for Asynchronous Pages . . . . . . . . . . . . . . . . . . . . . . . . . 206
The RegisterAsyncTask Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Building Custom Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
ASP-Style Code Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Data-Binding Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Dynamic Expressions in ASP.NET 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Writing a Custom Expression Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Building Parser Filters for ASP.NET Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
The PageParserFilter Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Real-World Scenarios. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Part
II Special Features
6 Working with Script Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Adding Client-Side Script to Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Checking Browser Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Registering Script Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Adding Popup Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Script Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
The ASP.NET Raw API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Executing an Out-of-Band Call. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
The Ajax.NET Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Using the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Ajax.NET vs. Script Callbacks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
A05T621772.fm Page ix Friday, February 10, 2006 2:54 PM