pro asp.net 4.5 in c# - rd.springer.com978-1-4302-4255-0/1.pdf · cover designer: anna ishchenko...
TRANSCRIPT
Pro ASP.NET 4.5 in C#
Adam Freeman
Pro ASP.NET 4.5 in C#
Copyright © 2013 by Adam Freeman
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4302-4254-3
ISBN-13 (electronic): 978-1-4302-4255-0
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul ManningLead Editor: Ewan BuckinghamTechnical Reviewer: Fabio Claudio FerracchiatiEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel,
Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh
Coordinating Editor: Christine RickettsCopy Editors: Ann Dickson and James ComptonCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.
This book is dedicated to the memory of my mother, Joan Freeman.3rd March 1950 – 25th February 2013
(And also dedicated to my wife, Jacqui Griffyth. She is very much alive. I have dedicated all of my books to her over the years, and I didn’t want to break with tradition.)
v
Contents at a Glance
About the Author ������������������������������������������������������������������������������������������������������������ xxxv
About the Technical Reviewer �������������������������������������������������������������������������������������� xxxvii
Acknowledgments ��������������������������������������������������������������������������������������������������������� xxxix
Part 1: Getting Started ■ ���������������������������������������������������������������������������������� 1
Chapter 1: Your First ASP�NET Application ■ �������������������������������������������������������������������������3
Chapter 2: Putting ASP�NET in Context ■ ����������������������������������������������������������������������������29
Chapter 3: Essential C# Language Features ■ ��������������������������������������������������������������������37
Chapter 4: Using jQuery ■ ���������������������������������������������������������������������������������������������������75
Chapter 5: Essential Development Tools ■ ������������������������������������������������������������������������101
Chapter 6: SportsStore: A Real Application ■ �������������������������������������������������������������������117
Chapter 7: SportsStore: Navigation & Cart ■ ��������������������������������������������������������������������143
Chapter 8: SportsStore: Completing the Cart ■ ����������������������������������������������������������������173
Chapter 9: SportsStore: Administration ■ ������������������������������������������������������������������������207
Chapter 10: SportsStore: Deployment ■ ���������������������������������������������������������������������������231
Chapter 11: Testable Web Apps ■ �������������������������������������������������������������������������������������247
Part 2: The Core ASP�NET Platform ■ ����������������������������������������������������������� 279
Chapter 12: Working with Web Forms ■ ���������������������������������������������������������������������������281
Chapter 13: Lifecycles and Context ■ �������������������������������������������������������������������������������313
Chapter 14: Modules ■ �����������������������������������������������������������������������������������������������������345
Chapter 15: Handlers ■ �����������������������������������������������������������������������������������������������������369
■ Contents at a GlanCe
vi
Chapter 16: Page and Control Lifecycle Events ■ �������������������������������������������������������������401
Chapter 17: Managing Request Execution ■ ���������������������������������������������������������������������425
Chapter 18: Managing State Data ■ ����������������������������������������������������������������������������������451
Chapter 19: Caching ■ ������������������������������������������������������������������������������������������������������487
Chapter 20: Caching Output ■ �������������������������������������������������������������������������������������������515
Chapter 21: Handling Errors ■ ������������������������������������������������������������������������������������������549
Chapter 22: Managing Paths ■ �����������������������������������������������������������������������������������������581
Chapter 23: URL Routing ■ ������������������������������������������������������������������������������������������������607
Chapter 24: Advanced URL Routing ■ �������������������������������������������������������������������������������639
Chapter 25: Authentication and Authorization ■ ��������������������������������������������������������������667
Chapter 26: Membership ■ �����������������������������������������������������������������������������������������������697
Chapter 27: ASP�NET Configuration ■ ��������������������������������������������������������������������������������725
Chapter 28: Asynchronous Request Handling ■ ���������������������������������������������������������������761
Part 3: Forms and Controls ■ ����������������������������������������������������������������������� 777
Chapter 29: Working with Controls ■ �������������������������������������������������������������������������������779
Chapter 30: Forms and Request Validation ■ �������������������������������������������������������������������813
Chapter 31: Creating Custom Controls ■ ��������������������������������������������������������������������������839
Chapter 32: Stateful Controls ■ ����������������������������������������������������������������������������������������867
Chapter 33: Server-Side HTML Elements ■ �����������������������������������������������������������������������901
Chapter 34: Model Binding ■ ��������������������������������������������������������������������������������������������931
Chapter 35: Data Binding ■ �����������������������������������������������������������������������������������������������961
Chapter 36: Basic Data Controls ■ ������������������������������������������������������������������������������������991
Chapter 37: Complex Data Controls ■ �����������������������������������������������������������������������������1021
Chapter 38: Other ASP�NET Controls ■ ����������������������������������������������������������������������������1051
■ Contents at a GlanCe
vii
Part 4: Client-Side Development ■ ������������������������������������������������������������� 1073
Chapter 39: Managing Scripts and Styles ■ �������������������������������������������������������������������1075
Chapter 40: Ajax and Web Services ■ �����������������������������������������������������������������������������1099
Chapter 41: Client-Side Validation ■ �������������������������������������������������������������������������������1125
Chapter 42: Targeting Mobile Devices ■ �������������������������������������������������������������������������1149
Index �������������������������������������������������������������������������������������������������������������������������������1165
ix
Contents
About the Author ������������������������������������������������������������������������������������������������������������ xxxv
About the Technical Reviewer �������������������������������������������������������������������������������������� xxxvii
Acknowledgments ��������������������������������������������������������������������������������������������������������� xxxix
Part 1: Getting Started ■ ���������������������������������������������������������������������������������� 1
Chapter 1: Your First ASP�NET Application ■ �������������������������������������������������������������������������3
Preparing Your Workstation �����������������������������������������������������������������������������������������������������������3
Creating a New ASP�NET Project ����������������������������������������������������������������������������������������������������3
Adding a New Web Form ��������������������������������������������������������������������������������������������������������������������������������������� 5
Testing the Example Application ���������������������������������������������������������������������������������������������������������������������������� 6
Creating a Simple Application �������������������������������������������������������������������������������������������������������9
Setting the Scene �������������������������������������������������������������������������������������������������������������������������������������������������� 9
Creating a Data Model and Repository ������������������������������������������������������������������������������������������������������������������ 9
Creating and Styling the Form ����������������������������������������������������������������������������������������������������������������������������� 11
Handling the Form ����������������������������������������������������������������������������������������������������������������������������������������������� 13
Creating the Summary View ��������������������������������������������������������������������������������������������������������18
Formatting the Dynamic HTML ���������������������������������������������������������������������������������������������������������������������������� 20
Testing the Dynamic Code ����������������������������������������������������������������������������������������������������������������������������������� 20
Calling a Code-Behind Method ���������������������������������������������������������������������������������������������������������������������������� 21
Performing Validation ������������������������������������������������������������������������������������������������������������������23
Summary �������������������������������������������������������������������������������������������������������������������������������������28
■ Contents
x
Chapter 2: Putting ASP�NET in Context ■ ����������������������������������������������������������������������������29
An Overview of the ASP�NET Framework �������������������������������������������������������������������������������������29
The Evolution and Restructuring of ASP�NET ������������������������������������������������������������������������������������������������������� 30
About This Book ��������������������������������������������������������������������������������������������������������������������������31
What Do You Need to Know? ������������������������������������������������������������������������������������������������������������������������������� 31
What Software and Technology Do I Need? ��������������������������������������������������������������������������������������������������������� 32
Are There Lots of Examples in This Book? ���������������������������������������������������������������������������������������������������������� 32
The Structure of This Book ����������������������������������������������������������������������������������������������������������32
Understanding Web Forms ����������������������������������������������������������������������������������������������������������33
Understanding Web Forms Strengths ������������������������������������������������������������������������������������������������������������������ 34
Understanding Web Forms Weaknesses �������������������������������������������������������������������������������������������������������������� 35
Summary �������������������������������������������������������������������������������������������������������������������������������������36
Chapter 3: Essential C# Language Features ■ ��������������������������������������������������������������������37
Creating the Example Project ������������������������������������������������������������������������������������������������������37
Using Automatically Implemented Properties �����������������������������������������������������������������������������38
Using Object and Collection Initializers ���������������������������������������������������������������������������������������41
Using Extension Methods ������������������������������������������������������������������������������������������������������������44
Applying Extension Methods to an Interface ������������������������������������������������������������������������������������������������������� 46
Creating Filtering Extension Methods ������������������������������������������������������������������������������������������������������������������ 48
Using Lambda Expressions ���������������������������������������������������������������������������������������������������������49
Using Automatic Type Inference ��������������������������������������������������������������������������������������������������53
Using Anonymous Types ��������������������������������������������������������������������������������������������������������������54
Using Generic Typing �������������������������������������������������������������������������������������������������������������������56
Using a Common Base Class ������������������������������������������������������������������������������������������������������������������������������� 58
Using Generic Typing ������������������������������������������������������������������������������������������������������������������������������������������� 59
Explicitly Implementing Interfaces ����������������������������������������������������������������������������������������������62
Performing Language Integrated Queries �����������������������������������������������������������������������������������64
Understanding Deferred LINQ Queries ����������������������������������������������������������������������������������������������������������������� 67
■ Contents
xi
Using Async Methods ������������������������������������������������������������������������������������������������������������������70
Applying the async and await Keywords ������������������������������������������������������������������������������������������������������������� 72
Summary �������������������������������������������������������������������������������������������������������������������������������������73
Chapter 4: Using jQuery ■ ���������������������������������������������������������������������������������������������������75
Creating the Example Project ������������������������������������������������������������������������������������������������������75
Adding jQuery to the Example Project ����������������������������������������������������������������������������������������������������������������� 77
Adding jQuery to the Web Form ��������������������������������������������������������������������������������������������������������������������������� 79
Getting Started with jQuery ���������������������������������������������������������������������������������������������������������80
Waiting for the DOM �������������������������������������������������������������������������������������������������������������������������������������������� 80
Understanding jQuery Statements ����������������������������������������������������������������������������������������������������������������������� 81
Selecting Elements ����������������������������������������������������������������������������������������������������������������������82
Selecting Elements by Type, Class, or ID ������������������������������������������������������������������������������������������������������������� 83
Selecting Elements Using Relationships and Unions ������������������������������������������������������������������������������������������� 83
Selecting Elements Using Attributes ������������������������������������������������������������������������������������������������������������������� 85
Selecting Elements Using Filters ������������������������������������������������������������������������������������������������������������������������� 86
Using jQuery Functions ���������������������������������������������������������������������������������������������������������������������������������������� 90
Using jQuery Events ��������������������������������������������������������������������������������������������������������������������94
Working with JSON Data �������������������������������������������������������������������������������������������������������������96
Summary �������������������������������������������������������������������������������������������������������������������������������������99
Chapter 5: Essential Development Tools ■ ������������������������������������������������������������������������101
Creating the Example Project ����������������������������������������������������������������������������������������������������101
Using the Visual Studio Debugger ���������������������������������������������������������������������������������������������103
Creating Conditional Breakpoints ���������������������������������������������������������������������������������������������������������������������� 104
Understanding the Application State ����������������������������������������������������������������������������������������������������������������� 106
Using the Immediate Window ���������������������������������������������������������������������������������������������������������������������������� 107
Using the Page Inspector and Browser F12 Tools ���������������������������������������������������������������������107
Using the JavaScript Console ���������������������������������������������������������������������������������������������������������������������������� 108
Using the Network Monitor �������������������������������������������������������������������������������������������������������������������������������� 110
Using the JavaScript Profiler ����������������������������������������������������������������������������������������������������������������������������� 111
■ Contents
xii
Using NuGet �������������������������������������������������������������������������������������������������������������������������������112
Using Opera Mobile �������������������������������������������������������������������������������������������������������������������114
Useful JavaScript Libraries �������������������������������������������������������������������������������������������������������115
jQuery, jQuery UI, and jQuery Mobile ����������������������������������������������������������������������������������������������������������������� 115
Knockout ������������������������������������������������������������������������������������������������������������������������������������������������������������ 116
Modernizr ���������������������������������������������������������������������������������������������������������������������������������������������������������� 116
requireJS ����������������������������������������������������������������������������������������������������������������������������������������������������������� 116
Summary �����������������������������������������������������������������������������������������������������������������������������������116
Chapter 6: SportsStore: A Real Application ■ �������������������������������������������������������������������117
Creating the Project �������������������������������������������������������������������������������������������������������������������117
Creating the Folder Structure ���������������������������������������������������������������������������������������������������������������������������� 117
Adding the Global Application Class ������������������������������������������������������������������������������������������������������������������ 119
Creating the Database ��������������������������������������������������������������������������������������������������������������������������������������� 121
Defining the Database Schema ������������������������������������������������������������������������������������������������������������������������� 123
Adding Data to the Database ����������������������������������������������������������������������������������������������������������������������������� 125
Creating the Data Model and Repository �����������������������������������������������������������������������������������126
Creating the Data Model Class �������������������������������������������������������������������������������������������������������������������������� 126
Adding the Entity Framework ���������������������������������������������������������������������������������������������������������������������������� 127
Creating the Entity Framework Context ������������������������������������������������������������������������������������������������������������� 127
Creating the Product Repository ������������������������������������������������������������������������������������������������������������������������ 129
Creating the Product Listing ������������������������������������������������������������������������������������������������������129
Adding Pagination ���������������������������������������������������������������������������������������������������������������������132
Displaying a Page of Products ��������������������������������������������������������������������������������������������������������������������������� 132
Adding Pagination Links ������������������������������������������������������������������������������������������������������������������������������������ 135
Styling the List Web Form ���������������������������������������������������������������������������������������������������������137
Creating a Master Page ������������������������������������������������������������������������������������������������������������������������������������� 138
Customizing the Master Page ���������������������������������������������������������������������������������������������������������������������������� 139
Applying the Master Page ���������������������������������������������������������������������������������������������������������������������������������� 140
Testing the Master Page ������������������������������������������������������������������������������������������������������������������������������������ 141
Summary �����������������������������������������������������������������������������������������������������������������������������������142
■ Contents
xiii
Chapter 7: SportsStore: Navigation & Cart ■ ��������������������������������������������������������������������143
Configuring URL Routing �����������������������������������������������������������������������������������������������������������143
Creating the Routing Configuration Class ���������������������������������������������������������������������������������������������������������� 144
Updating the Global Application Class ��������������������������������������������������������������������������������������������������������������� 146
Using Routing Variables ������������������������������������������������������������������������������������������������������������������������������������� 146
Testing the Routing Configuration ��������������������������������������������������������������������������������������������������������������������� 147
Generating Routed Links ����������������������������������������������������������������������������������������������������������������������������������� 148
Adding the Category Information ����������������������������������������������������������������������������������������������149
Creating the User Control ���������������������������������������������������������������������������������������������������������������������������������� 150
Applying the User Control to the Master Page ��������������������������������������������������������������������������������������������������� 152
Adding the CSS Styles ��������������������������������������������������������������������������������������������������������������������������������������� 153
Expanding the URL Scheme������������������������������������������������������������������������������������������������������������������������������� 153
Adding Support for Displaying Categories ��������������������������������������������������������������������������������������������������������� 155
Highlighting the Current Category ��������������������������������������������������������������������������������������������������������������������� 157
Building the Shopping Cart��������������������������������������������������������������������������������������������������������159
Defining the Cart Class �������������������������������������������������������������������������������������������������������������������������������������� 160
Adding the Cart Buttons������������������������������������������������������������������������������������������������������������������������������������� 161
Creating a Session Helper ��������������������������������������������������������������������������������������������������������������������������������� 164
Handling the Form Post ������������������������������������������������������������������������������������������������������������������������������������� 167
Displaying the Contents of the Cart ������������������������������������������������������������������������������������������������������������������� 168
Testing the Cart ������������������������������������������������������������������������������������������������������������������������������������������������� 171
Summary �����������������������������������������������������������������������������������������������������������������������������������171
Chapter 8: SportsStore: Completing the Cart ■ ����������������������������������������������������������������173
Removing Unwanted Cart Items ������������������������������������������������������������������������������������������������173
Understanding View State ��������������������������������������������������������������������������������������������������������������������������������� 175
Disabling View State ������������������������������������������������������������������������������������������������������������������������������������������ 176
Adding the Cart Summary ���������������������������������������������������������������������������������������������������������177
Defining the CSS Styles ������������������������������������������������������������������������������������������������������������������������������������� 179
Applying the Cart Summary Control ������������������������������������������������������������������������������������������������������������������ 179
Consolidating User Control Declarations ����������������������������������������������������������������������������������������������������������� 180
■ Contents
xiv
Submitting Orders ���������������������������������������������������������������������������������������������������������������������181
Extending the Database and Data Model ����������������������������������������������������������������������������������������������������������� 181
Adding the Checkout Link and URL ������������������������������������������������������������������������������������������������������������������� 185
Processing the Order ����������������������������������������������������������������������������������������������������������������������������������������� 188
Adding Validation �����������������������������������������������������������������������������������������������������������������������193
Adding the NuGet Packages ������������������������������������������������������������������������������������������������������������������������������ 195
Creating and Using a Script Bundle ������������������������������������������������������������������������������������������������������������������� 197
Setting up Client-Side Validation ����������������������������������������������������������������������������������������������������������������������� 199
Creating a Server Control ���������������������������������������������������������������������������������������������������������������������������������� 201
Applying the Server Control ������������������������������������������������������������������������������������������������������������������������������� 203
Summary �����������������������������������������������������������������������������������������������������������������������������������205
Chapter 9: SportsStore: Administration ■ ������������������������������������������������������������������������207
Adding the Common Building Blocks ����������������������������������������������������������������������������������������207
Extending the Routing Configuration ����������������������������������������������������������������������������������������������������������������� 207
Adding the Admin Master Page ������������������������������������������������������������������������������������������������������������������������� 208
Adding the CSS Style Sheet ������������������������������������������������������������������������������������������������������������������������������� 209
Adding a Web Form ������������������������������������������������������������������������������������������������������������������������������������������� 210
Adding Order Management �������������������������������������������������������������������������������������������������������211
Cleansing and Populating the Database ������������������������������������������������������������������������������������������������������������ 212
Adding the Web Form Content ��������������������������������������������������������������������������������������������������������������������������� 212
Creating the Code-Behind Class ������������������������������������������������������������������������������������������������������������������������ 216
Adding Catalog Management ����������������������������������������������������������������������������������������������������218
Extending the Repository ����������������������������������������������������������������������������������������������������������������������������������� 218
Adding the Web Form ���������������������������������������������������������������������������������������������������������������������������������������� 220
Setting up the CRUD Methods ��������������������������������������������������������������������������������������������������������������������������� 221
Testing Catalog Management ���������������������������������������������������������������������������������������������������������������������������� 223
Setting up Authorization ������������������������������������������������������������������������������������������������������������224
Securing the Administration Pages ������������������������������������������������������������������������������������������������������������������� 224
Creating the Authentication Login Web Form ���������������������������������������������������������������������������������������������������� 225
■ Contents
xv
Testing Failed Authentication ���������������������������������������������������������������������������������������������������������������������������� 227
Testing Successful Authentication and Authorization ���������������������������������������������������������������������������������������� 228
Summary �����������������������������������������������������������������������������������������������������������������������������������229
Chapter 10: SportsStore: Deployment ■ ���������������������������������������������������������������������������231
Disabling Debug Mode for Final Testing ������������������������������������������������������������������������������������231
Preparing Windows Azure ���������������������������������������������������������������������������������������������������������232
Creating the Web Site and Database ����������������������������������������������������������������������������������������������������������������� 233
Preparing the Database for Remote Administration ������������������������������������������������������������������������������������������ 236
Creating the Schema ����������������������������������������������������������������������������������������������������������������������������������������� 237
Deploying the Application ����������������������������������������������������������������������������������������������������������240
Summary �����������������������������������������������������������������������������������������������������������������������������������246
Chapter 11: Testable Web Apps ■ �������������������������������������������������������������������������������������247
Understanding the Problem �������������������������������������������������������������������������������������������������������247
Understanding the Solution �������������������������������������������������������������������������������������������������������248
Why Not Just Use MVC? ������������������������������������������������������������������������������������������������������������������������������������ 250
Creating the Example Project ����������������������������������������������������������������������������������������������������250
Setting Up the Static Content ���������������������������������������������������������������������������������������������������������������������������� 251
Setting Up the Data Model ��������������������������������������������������������������������������������������������������������������������������������� 252
Implementing the Repository ���������������������������������������������������������������������������������������������������������������������������� 252
Adding the Infrastructure ����������������������������������������������������������������������������������������������������������������������������������� 253
Implementing the RSVP Page ����������������������������������������������������������������������������������������������������255
Creating the Presenter ��������������������������������������������������������������������������������������������������������������������������������������� 255
Creating the View ���������������������������������������������������������������������������������������������������������������������������������������������� 257
Testing the RSVP Page ��������������������������������������������������������������������������������������������������������������������������������������� 259
Adding Unit Testing ��������������������������������������������������������������������������������������������������������������������260
Creating the Unit Test Project ���������������������������������������������������������������������������������������������������������������������������� 260
Creating Unit Tests ��������������������������������������������������������������������������������������������������������������������������������������������� 262
Testing Input Values ������������������������������������������������������������������������������������������������������������������������������������������� 265
■ Contents
xvi
Adding Dependency Injection ����������������������������������������������������������������������������������������������������270
Adding the Ninject Package������������������������������������������������������������������������������������������������������������������������������� 271
Configuring Injection ����������������������������������������������������������������������������������������������������������������������������������������� 273
Completing the Application �������������������������������������������������������������������������������������������������������275
Creating the Presenter ��������������������������������������������������������������������������������������������������������������������������������������� 276
Configuring Dependency Injection ��������������������������������������������������������������������������������������������������������������������� 277
Summary �����������������������������������������������������������������������������������������������������������������������������������278
Part 2: The Core ASP�NET Platform ■ ����������������������������������������������������������� 279
Chapter 12: Working with Web Forms ■ ���������������������������������������������������������������������������281
Creating the Example Project ����������������������������������������������������������������������������������������������������281
Understanding the Web Form File ���������������������������������������������������������������������������������������������282
Using Code Nuggets ������������������������������������������������������������������������������������������������������������������������������������������ 283
Understanding Programmable HTML Elements ������������������������������������������������������������������������������������������������� 293
Understanding Controls ������������������������������������������������������������������������������������������������������������������������������������� 295
Understanding Code-Behind Classes ����������������������������������������������������������������������������������������295
Avoiding Duplication in Code-Behind Classes ��������������������������������������������������������������������������������������������������� 296
Understanding How a Web Form Works ������������������������������������������������������������������������������������298
Handling Programmable HTML Elements ���������������������������������������������������������������������������������������������������������� 299
Compiling the Web Form ����������������������������������������������������������������������������������������������������������������������������������� 300
Using Master Pages ������������������������������������������������������������������������������������������������������������������302
Configuring Master Pages ��������������������������������������������������������������������������������������������������������������������������������� 303
Understanding Master Page Placeholders ��������������������������������������������������������������������������������������������������������� 304
Applying the Master Page ���������������������������������������������������������������������������������������������������������������������������������� 305
Using the Master Page Code-Behind Class ������������������������������������������������������������������������������������������������������� 306
Nesting Master Pages���������������������������������������������������������������������������������������������������������������������������������������� 308
Summary �����������������������������������������������������������������������������������������������������������������������������������311
Chapter 13: Lifecycles and Context ■ �������������������������������������������������������������������������������313
Creating the Example Project ����������������������������������������������������������������������������������������������������313
Understanding the Global Application Class ������������������������������������������������������������������������������������������������������ 315
Understanding the Application Lifecycle �����������������������������������������������������������������������������������317
■ Contents
xvii
Understanding the Request Lifecycle ��������������������������������������������������������������������������������������� 320
Understanding Modules and Handlers���������������������������������������������������������������������������������������������������������������322
Handling Request Lifecycle Events ��������������������������������������������������������������������������������������������������������������������324
Handling Multiple Events in a Method ���������������������������������������������������������������������������������������������������������������326
Understanding Context Objects ������������������������������������������������������������������������������������������������ 329
Working with HttpContext Objects ���������������������������������������������������������������������������������������������������������������������330
Working with HttpApplication Objects ��������������������������������������������������������������������������������������������������������������331
Working with HttpRequest Objects ��������������������������������������������������������������������������������������������������������������������335
Working with HttpResponse Objects �����������������������������������������������������������������������������������������������������������������337
Putting It All Together ��������������������������������������������������������������������������������������������������������������� 340
Timing the Request ��������������������������������������������������������������������������������������������������������������������������������������������341
Restricting Access ���������������������������������������������������������������������������������������������������������������������������������������������341
Logging the Request ������������������������������������������������������������������������������������������������������������������������������������������343
Summary ���������������������������������������������������������������������������������������������������������������������������������� 343
Chapter 14: Modules ■ ���������������������������������������������������������������������������������������������������� 345
Preparing the Example Application ������������������������������������������������������������������������������������������ 345
Understanding Modules ����������������������������������������������������������������������������������������������������������� 347
Creating a Module ���������������������������������������������������������������������������������������������������������������������������������������������347
Registering a Module �����������������������������������������������������������������������������������������������������������������������������������������349
Creating a Module Project �������������������������������������������������������������������������������������������������������� 349
Creating the Visual Studio Project ���������������������������������������������������������������������������������������������������������������������350
Creating the Modules ����������������������������������������������������������������������������������������������������������������������������������������351
Registering the Modules ������������������������������������������������������������������������������������������������������������������������������������352
Working with Module Events ���������������������������������������������������������������������������������������������������� 354
Defining the Module Event���������������������������������������������������������������������������������������������������������������������������������354
Handling the Module Event ��������������������������������������������������������������������������������������������������������������������������������355
Locating Modules by Name �������������������������������������������������������������������������������������������������������������������������������357
Working with the Built-In Modules ������������������������������������������������������������������������������������������� 361
Putting It All Together ��������������������������������������������������������������������������������������������������������������� 365
Summary ���������������������������������������������������������������������������������������������������������������������������������� 368
■ Contents
xviii
Chapter 15: Handlers ■ �����������������������������������������������������������������������������������������������������369
Preparing the Example Application �������������������������������������������������������������������������������������������369
Understanding Handlers ������������������������������������������������������������������������������������������������������������371
Handlers and the Request Lifecycle ������������������������������������������������������������������������������������������372
Creating a Generic Handler �������������������������������������������������������������������������������������������������������373
Implementing Custom Behavior ������������������������������������������������������������������������������������������������������������������������ 375
Testing the Generic Handler ������������������������������������������������������������������������������������������������������������������������������ 377
Creating Custom Handlers���������������������������������������������������������������������������������������������������������378
Creating a Custom Handler �������������������������������������������������������������������������������������������������������������������������������� 379
Registering a Custom Handler ��������������������������������������������������������������������������������������������������������������������������� 379
Creating Custom Handler Factories �������������������������������������������������������������������������������������������381
Controlling Handler Instantiation ����������������������������������������������������������������������������������������������������������������������� 382
Selecting Handlers Dynamically ������������������������������������������������������������������������������������������������������������������������ 384
Recycling Handlers �������������������������������������������������������������������������������������������������������������������������������������������� 385
Coordinating between Modules and Handlers ��������������������������������������������������������������������������388
Using the Items Collection ��������������������������������������������������������������������������������������������������������������������������������� 389
Using Declarative Interfaces ������������������������������������������������������������������������������������������������������������������������������ 392
Putting It All Together ����������������������������������������������������������������������������������������������������������������393
Finding the Right Built-In Handler Factory �������������������������������������������������������������������������������������������������������� 395
Building on the Base Class �������������������������������������������������������������������������������������������������������������������������������� 395
Writing the Handler �������������������������������������������������������������������������������������������������������������������������������������������� 396
Registering the Handler Factory ������������������������������������������������������������������������������������������������������������������������ 397
Testing the Handler Factory ������������������������������������������������������������������������������������������������������������������������������� 398
Summary �����������������������������������������������������������������������������������������������������������������������������������399
Chapter 16: Page and Control Lifecycle Events ■ �������������������������������������������������������������401
Preparing the Example Application �������������������������������������������������������������������������������������������401
Understanding the Page Class ��������������������������������������������������������������������������������������������������403
Recreating the Handler Factory ������������������������������������������������������������������������������������������������������������������������� 404
Understanding the Page Lifecycle ���������������������������������������������������������������������������������������������406
Handling the Page Events ���������������������������������������������������������������������������������������������������������������������������������� 407
■ Contents
xix
Handling Control Events ������������������������������������������������������������������������������������������������������������409
Creating a Simple Control ���������������������������������������������������������������������������������������������������������������������������������� 410
Registering and Applying the Control ���������������������������������������������������������������������������������������������������������������� 412
Receiving Control Events �����������������������������������������������������������������������������������������������������������413
Handling the Control Event �������������������������������������������������������������������������������������������������������������������������������� 414
Understanding the End-to-End Web Lifecycle ���������������������������������������������������������������������������418
The Page Context ����������������������������������������������������������������������������������������������������������������������419
Getting Access to Context Objects ��������������������������������������������������������������������������������������������������������������������� 419
Setting the Page Directive Values ���������������������������������������������������������������������������������������������������������������������� 420
Providing Web Form-Specific Information ��������������������������������������������������������������������������������������������������������� 421
Putting It All Together ����������������������������������������������������������������������������������������������������������������422
Summary �����������������������������������������������������������������������������������������������������������������������������������424
Chapter 17: Managing Request Execution ■ ���������������������������������������������������������������������425
Preparing the Example Application �������������������������������������������������������������������������������������������425
Using URL Redirection ���������������������������������������������������������������������������������������������������������������427
Performing URL Redirection ������������������������������������������������������������������������������������������������������������������������������ 428
Manually Performing Redirections �������������������������������������������������������������������������������������������������������������������� 431
Managing Handler Selection and Execution ������������������������������������������������������������������������������432
Preempting Handler Selection ��������������������������������������������������������������������������������������������������������������������������� 433
Transferring a Request �������������������������������������������������������������������������������������������������������������������������������������� 435
Composing Responses by Explicitly Executing Handlers����������������������������������������������������������������������������������� 438
Putting It All Together ����������������������������������������������������������������������������������������������������������������439
Creating the Source Code View Handler ������������������������������������������������������������������������������������������������������������ 439
Using an HTTP Redirection �������������������������������������������������������������������������������������������������������������������������������� 441
Remapping the Handler ������������������������������������������������������������������������������������������������������������������������������������� 442
Executing Multiple Handlers ������������������������������������������������������������������������������������������������������������������������������ 444
Summary �����������������������������������������������������������������������������������������������������������������������������������449
Chapter 18: Managing State Data ■ ����������������������������������������������������������������������������������451
Creating the Example Application ���������������������������������������������������������������������������������������������451
Understanding State Data ���������������������������������������������������������������������������������������������������������452
■ Contents
xx
Storing Application Data ������������������������������������������������������������������������������������������������������������453
Storing User Data ����������������������������������������������������������������������������������������������������������������������455
Creating the Profile Database ����������������������������������������������������������������������������������������������������������������������������456
Configuring the Database Connection ���������������������������������������������������������������������������������������������������������������458
Configuring Profiles and Profile Properties��������������������������������������������������������������������������������������������������������459
Defining Profile Providers ����������������������������������������������������������������������������������������������������������������������������������460
Defining Profile Properties ���������������������������������������������������������������������������������������������������������������������������������461
Using Profile Data ����������������������������������������������������������������������������������������������������������������������������������������������463
Storing Session Data �����������������������������������������������������������������������������������������������������������������466
Using Session Data ��������������������������������������������������������������������������������������������������������������������������������������������466
Configuring Session Data ����������������������������������������������������������������������������������������������������������������������������������470
Using the State Server ���������������������������������������������������������������������������������������������������������������������������������������471
Using a SQL Database ���������������������������������������������������������������������������������������������������������������������������������������473
Using View Data ������������������������������������������������������������������������������������������������������������������������476
Configuring View State ��������������������������������������������������������������������������������������������������������������������������������������478
Using Cookies ����������������������������������������������������������������������������������������������������������������������������480
Putting It All Together ����������������������������������������������������������������������������������������������������������������481
Creating the Module ������������������������������������������������������������������������������������������������������������������������������������������482
Creating the Web Form ��������������������������������������������������������������������������������������������������������������������������������������483
Summary �����������������������������������������������������������������������������������������������������������������������������������486
Chapter 19: Caching ■ ������������������������������������������������������������������������������������������������������487
Preparing the Example Application �������������������������������������������������������������������������������������������487
Using the Application Cache ������������������������������������������������������������������������������������������������������490
Managing Item Caching ������������������������������������������������������������������������������������������������������������492
Caching with Dependencies ������������������������������������������������������������������������������������������������������493
Caching with an Internal Dependency ���������������������������������������������������������������������������������������������������������������496
Creating a Custom Dependency ������������������������������������������������������������������������������������������������������������������������497
Caching with Aggregate Dependencies �������������������������������������������������������������������������������������������������������������500
Caching with Expiration Constraints ������������������������������������������������������������������������������������������������������������������501
Caching with Scavenging Prioritization �������������������������������������������������������������������������������������������������������������502
■ Contents
xxi
Receiving Cache Notifications ���������������������������������������������������������������������������������������������������504
Receiving Notification of Cache Ejection ����������������������������������������������������������������������������������������������������������� 505
Using Notifications to Prevent Cache Ejection ��������������������������������������������������������������������������������������������������� 507
Configuring Caching ������������������������������������������������������������������������������������������������������������������509
Putting It All Together ����������������������������������������������������������������������������������������������������������������511
Summary �����������������������������������������������������������������������������������������������������������������������������������513
Chapter 20: Caching Output ■ �������������������������������������������������������������������������������������������515
Preparing the Example Application �������������������������������������������������������������������������������������������515
Caching Web Form Output ���������������������������������������������������������������������������������������������������������517
Controlling End-to-End Caching ������������������������������������������������������������������������������������������������������������������������ 519
Caching Multiple Copies of Content ������������������������������������������������������������������������������������������������������������������� 519
Creating Cache Profiles ������������������������������������������������������������������������������������������������������������������������������������� 523
Selectively Updating Content ����������������������������������������������������������������������������������������������������524
Caching User Control Output �����������������������������������������������������������������������������������������������������527
Caching Multiple Copies Based on Nested Controls ������������������������������������������������������������������������������������������ 530
Caching Server Control Output ��������������������������������������������������������������������������������������������������531
Creating Cache Dependencies ��������������������������������������������������������������������������������������������������533
Using a Custom Output Cache ���������������������������������������������������������������������������������������������������535
Creating the Custom Cache Implementation ����������������������������������������������������������������������������������������������������� 535
Registering the Custom Output Cache Implementation ������������������������������������������������������������������������������������� 537
Dynamically Selecting an Output Cache Implementation ���������������������������������������������������������������������������������� 538
Configuring the Output Cache ���������������������������������������������������������������������������������������������������540
Putting It All Together ����������������������������������������������������������������������������������������������������������������541
Creating the Handler Factory Class ������������������������������������������������������������������������������������������������������������������� 542
Registering the Handler Factory ������������������������������������������������������������������������������������������������������������������������ 546
Summary �����������������������������������������������������������������������������������������������������������������������������������547
Chapter 21: Handling Errors ■ ������������������������������������������������������������������������������������������549
Preparing the Example Project ��������������������������������������������������������������������������������������������������549
Understanding Errors ����������������������������������������������������������������������������������������������������������������552
■ Contents
xxii
Customizing the Default Behavior ���������������������������������������������������������������������������������������������554
Providing a Catchall Error Page ������������������������������������������������������������������������������������������������������������������������� 555
Handling Specific HTTP Errors ��������������������������������������������������������������������������������������������������������������������������� 559
Specifying an Error Page Specific to a Web Form ��������������������������������������������������������������������������������������������� 566
Taking Control of the Error Handling Process ����������������������������������������������������������������������������567
Handling the Error in the Web Form ������������������������������������������������������������������������������������������������������������������ 567
Handling the Error at the Application Level ������������������������������������������������������������������������������������������������������� 570
Handling Errors without Redirection ������������������������������������������������������������������������������������������������������������������ 572
Handling Multiple Errors ������������������������������������������������������������������������������������������������������������573
Reporting the Errors ������������������������������������������������������������������������������������������������������������������������������������������ 574
Displaying the Errors ����������������������������������������������������������������������������������������������������������������������������������������� 575
Intercepting the Errors ��������������������������������������������������������������������������������������������������������������������������������������� 576
Putting It All Together ����������������������������������������������������������������������������������������������������������������578
Removing the Existing Error Handling Code ������������������������������������������������������������������������������������������������������ 578
Defining the Module ������������������������������������������������������������������������������������������������������������������������������������������ 578
Summary �����������������������������������������������������������������������������������������������������������������������������������580
Chapter 22: Managing Paths ■ �����������������������������������������������������������������������������������������581
Preparing the Example Project ��������������������������������������������������������������������������������������������������581
Creating a Module ��������������������������������������������������������������������������������������������������������������������������������������������� 582
Creating Additional Content ������������������������������������������������������������������������������������������������������������������������������� 583
Working with Paths �������������������������������������������������������������������������������������������������������������������583
Getting Path Information ������������������������������������������������������������������������������������������������������������������������������������ 584
Manipulating Paths �������������������������������������������������������������������������������������������������������������������������������������������� 588
Managing Virtual Paths �������������������������������������������������������������������������������������������������������������589
Setting Default Documents �������������������������������������������������������������������������������������������������������������������������������� 590
Handling Requests for Extensionless URLs ������������������������������������������������������������������������������������������������������� 592
Rewriting Paths ������������������������������������������������������������������������������������������������������������������������������������������������� 594
Using the Friendly URLs Package ����������������������������������������������������������������������������������������������596
Disabling the Previous Examples����������������������������������������������������������������������������������������������������������������������� 596
Installing and Configuring the NuGet Package �������������������������������������������������������������������������������������������������� 597
Using the FriendlyUrls Library Features ������������������������������������������������������������������������������������������������������������ 597
■ Contents
xxiii
Putting It All Together ����������������������������������������������������������������������������������������������������������������601
Writing Files ������������������������������������������������������������������������������������������������������������������������������������������������������� 602
Rewriting Paths ������������������������������������������������������������������������������������������������������������������������������������������������� 603
Summary �����������������������������������������������������������������������������������������������������������������������������������605
Chapter 23: URL Routing ■ ������������������������������������������������������������������������������������������������607
Preparing the Example Project ��������������������������������������������������������������������������������������������������607
Preparing the Application for Routing ���������������������������������������������������������������������������������������608
Working with Fixed Routes ��������������������������������������������������������������������������������������������������������610
Getting Route Information ��������������������������������������������������������������������������������������������������������������������������������� 613
Adding Variable Segments ��������������������������������������������������������������������������������������������������������615
Dealing with Over-Eager Routes ������������������������������������������������������������������������������������������������������������������������ 616
Creating Hackable URLs ������������������������������������������������������������������������������������������������������������������������������������ 618
Creating Variable-Length Segments ������������������������������������������������������������������������������������������������������������������ 625
Model Binding to Route Segment Values ����������������������������������������������������������������������������������626
Generating Outgoing URLs ��������������������������������������������������������������������������������������������������������629
Manually Generating Outgoing URLs ����������������������������������������������������������������������������������������������������������������� 631
Putting It All Together ����������������������������������������������������������������������������������������������������������������632
Generating the Diagnostic HTML ����������������������������������������������������������������������������������������������������������������������� 633
Testing URL Matching ���������������������������������������������������������������������������������������������������������������������������������������� 637
Summary �����������������������������������������������������������������������������������������������������������������������������������637
Chapter 24: Advanced URL Routing ■ �������������������������������������������������������������������������������639
Preparing the Example Project ��������������������������������������������������������������������������������������������������639
Using Advanced Constraints ������������������������������������������������������������������������������������������������������640
Restricting a Route by HTTP Method ����������������������������������������������������������������������������������������������������������������� 640
Creating a Custom Route Restriction ����������������������������������������������������������������������������������������������������������������� 642
Routing Requests for Files ��������������������������������������������������������������������������������������������������������644
Disabling File Requests for Individual Routes ��������������������������������������������������������������������������������������������������� 646
Working with Routing Handlers �������������������������������������������������������������������������������������������������647
Preventing Routing for a Request ���������������������������������������������������������������������������������������������������������������������� 648
Creating a Custom Route Handler ��������������������������������������������������������������������������������������������������������������������� 652
■ Contents
xxiv
Creating a Custom RouteBase Implementation �������������������������������������������������������������������������654
Putting It All Together ����������������������������������������������������������������������������������������������������������������657
Disabling ASPX Requests ����������������������������������������������������������������������������������������������������������������������������������� 657
Routing to Other File Types �������������������������������������������������������������������������������������������������������������������������������� 659
Letting ASP�NET Select the Route for an Outgoing URL ������������������������������������������������������������������������������������� 663
Summary �����������������������������������������������������������������������������������������������������������������������������������666
Chapter 25: Authentication and Authorization ■ ��������������������������������������������������������������667
Preparing the Example Project ��������������������������������������������������������������������������������������������������667
Understanding Forms Authentication ����������������������������������������������������������������������������������������669
Configuring ASP�NET Authentication ������������������������������������������������������������������������������������������670
Performing Authentication ��������������������������������������������������������������������������������������������������������672
Authenticating the User ������������������������������������������������������������������������������������������������������������������������������������� 674
Getting Authentication Information �������������������������������������������������������������������������������������������������������������������� 676
Performing Authorization ����������������������������������������������������������������������������������������������������������677
Understanding Authorization and Authentication Integration ���������������������������������������������������������������������������� 678
Creating an Authorization Policy ������������������������������������������������������������������������������������������������������������������������ 679
Creating Location-Specific Authorization Policies ��������������������������������������������������������������������������������������������� 684
Bypassing Authorization ������������������������������������������������������������������������������������������������������������686
Authorization Routed URLs ��������������������������������������������������������������������������������������������������������689
Putting It All Together ����������������������������������������������������������������������������������������������������������������691
Rebuilding the Authentication Web Form ���������������������������������������������������������������������������������������������������������� 691
Adding a Master Page ��������������������������������������������������������������������������������������������������������������������������������������� 692
Testing the Revised Authorization and Authentication ��������������������������������������������������������������������������������������� 695
Summary �����������������������������������������������������������������������������������������������������������������������������������696
Chapter 26: Membership ■ �����������������������������������������������������������������������������������������������697
Preparing the Example Project ��������������������������������������������������������������������������������������������������697
Adding Membership to the Application �������������������������������������������������������������������������������������698
Installing the Universal Providers ���������������������������������������������������������������������������������������������������������������������� 698
Configuring the Application for Membership ����������������������������������������������������������������������������������������������������� 699
■ Contents
xxv
Creating Users and Roles ���������������������������������������������������������������������������������������������������������������������������������� 704
Performing Authentication Using Membership �������������������������������������������������������������������������������������������������� 705
Using Membership ��������������������������������������������������������������������������������������������������������������������706
Performing Password Change ��������������������������������������������������������������������������������������������������������������������������� 708
Performing Password Recovery ������������������������������������������������������������������������������������������������������������������������ 711
Performing Registration ������������������������������������������������������������������������������������������������������������������������������������ 716
Putting It All Together ����������������������������������������������������������������������������������������������������������������721
Summary �����������������������������������������������������������������������������������������������������������������������������������724
Chapter 27: ASP�NET Configuration ■ ��������������������������������������������������������������������������������725
Preparing the Example Project ��������������������������������������������������������������������������������������������������725
Understanding the Configuration Hierarchy ������������������������������������������������������������������������������726
Getting Configuration Information Programmatically ����������������������������������������������������������������728
Working with Application Settings ��������������������������������������������������������������������������������������������������������������������� 728
Working with Connection Strings ���������������������������������������������������������������������������������������������������������������������� 732
Working with Configuration Sections ���������������������������������������������������������������������������������������������������������������� 736
Working with the Complete Configuration ��������������������������������������������������������������������������������������������������������� 740
Creating Custom Configuration Sections and Groups ���������������������������������������������������������������743
Creating a Simple Configuration Section ����������������������������������������������������������������������������������������������������������� 743
Creating a Collection Configuration Section ������������������������������������������������������������������������������������������������������ 748
Creating a Configuration Section Group ������������������������������������������������������������������������������������������������������������ 752
Using External Configuration Files ��������������������������������������������������������������������������������������������754
Locking Configuration Sections �������������������������������������������������������������������������������������������������755
Putting It All Together ����������������������������������������������������������������������������������������������������������������759
Summary �����������������������������������������������������������������������������������������������������������������������������������760
Chapter 28: Asynchronous Request Handling ■ ���������������������������������������������������������������761
Preparing the Example Project ��������������������������������������������������������������������������������������������������761
Understanding the Problem �������������������������������������������������������������������������������������������������������763
■ Contents
xxvi
Creating an Asynchronous Web Form ���������������������������������������������������������������������������������������765
Using an Asynchronous Method ������������������������������������������������������������������������������������������������������������������������ 767
Creating and Registering the Asynchronous Page Task������������������������������������������������������������������������������������� 767
Performing Multiple Tasks ��������������������������������������������������������������������������������������������������������������������������������� 768
Creating Asynchronous Modules �����������������������������������������������������������������������������������������������771
Creating Asynchronous Handlers ����������������������������������������������������������������������������������������������774
Summary �����������������������������������������������������������������������������������������������������������������������������������775
Part 3: Forms and Controls ■ ����������������������������������������������������������������������� 777
Chapter 29: Working with Controls ■ �������������������������������������������������������������������������������779
Preparing the Example Project ��������������������������������������������������������������������������������������������������779
Understanding Controls �������������������������������������������������������������������������������������������������������������780
Understanding the Base Control Class �������������������������������������������������������������������������������������������������������������� 781
Using Controls for Programmatic Access to HTML Elements ���������������������������������������������������������������������������� 782
Using Custom Controls to Generate Fragments of HTML����������������������������������������������������������������������������������� 783
Using Custom Server Controls ��������������������������������������������������������������������������������������������������������������������������� 788
Using Controls to Display Data �������������������������������������������������������������������������������������������������������������������������� 792
Using Controls to Model Desktop Development ������������������������������������������������������������������������������������������������ 795
Working with the Control Hierarchy ������������������������������������������������������������������������������������������799
Navigating the Control Hierarchy ����������������������������������������������������������������������������������������������������������������������� 799
Locating and Manipulating Controls in the Hierarchy ���������������������������������������������������������������������������������������� 802
Adding Controls Programmatically �������������������������������������������������������������������������������������������������������������������� 805
Putting It All Together ����������������������������������������������������������������������������������������������������������������807
Removing the Rich UI Controls �������������������������������������������������������������������������������������������������������������������������� 810
Summary �����������������������������������������������������������������������������������������������������������������������������������811
Chapter 30: Forms and Request Validation ■ �������������������������������������������������������������������813
Preparing the Example Project ��������������������������������������������������������������������������������������������������813
Adding jQuery ���������������������������������������������������������������������������������������������������������������������������������������������������� 814
Understanding the Server-Side Form Element ��������������������������������������������������������������������������814
Using the DefaultButton and DefaultFocus Properties �������������������������������������������������������������������������������������� 816
■ Contents
xxvii
Detecting Form Posts and Postbacks ����������������������������������������������������������������������������������������818
Looking for Form Data in the Query String �������������������������������������������������������������������������������������������������������� 819
Checking for POST Requests ����������������������������������������������������������������������������������������������������������������������������� 820
Working with Form Data ������������������������������������������������������������������������������������������������������������821
Understanding the One-Form Limit �������������������������������������������������������������������������������������������825
Understanding Request Validation ��������������������������������������������������������������������������������������������828
Using Eager Request Validation ������������������������������������������������������������������������������������������������������������������������� 829
Using Lazy Request Validation ��������������������������������������������������������������������������������������������������������������������������� 831
Using Unvalidated Form Data ���������������������������������������������������������������������������������������������������������������������������� 832
Request Validation in Controls ��������������������������������������������������������������������������������������������������������������������������� 835
Putting It All Together ����������������������������������������������������������������������������������������������������������������837
Summary �����������������������������������������������������������������������������������������������������������������������������������838
Chapter 31: Creating Custom Controls ■ ��������������������������������������������������������������������������839
Preparing the Example Project ��������������������������������������������������������������������������������������������������839
Adding jQuery ���������������������������������������������������������������������������������������������������������������������������������������������������� 840
Creating a Basic Control ������������������������������������������������������������������������������������������������������������841
Understanding the Control Directive ������������������������������������������������������������������������������������������������������������������ 841
Registering and Applying a Control ������������������������������������������������������������������������������������������������������������������� 842
Registering a Control in the Web�config File ������������������������������������������������������������������������������������������������������ 844
Adding Functionality to the Control �������������������������������������������������������������������������������������������845
Understanding Control IDs���������������������������������������������������������������������������������������������������������847
Identifying HTML Elements Generated by Controls ������������������������������������������������������������������������������������������� 848
Identifying Controls within the Control Hierarchy ���������������������������������������������������������������������������������������������� 849
Defining Element Attributes�������������������������������������������������������������������������������������������������������850
Creating Enumeration Attributes ����������������������������������������������������������������������������������������������������������������������� 852
Creating Collection Attributes ���������������������������������������������������������������������������������������������������������������������������� 854
■ Contents
xxviii
Creating a Server Control ����������������������������������������������������������������������������������������������������������857
Registering a Server Control ����������������������������������������������������������������������������������������������������������������������������� 859
Using the HtmlTextWriter Class ������������������������������������������������������������������������������������������������������������������������� 861
Putting It All Together ����������������������������������������������������������������������������������������������������������������864
Summary �����������������������������������������������������������������������������������������������������������������������������������865
Chapter 32: Stateful Controls ■ ����������������������������������������������������������������������������������������867
Preparing the Example Project ��������������������������������������������������������������������������������������������������867
Registering and Applying the User Control �������������������������������������������������������������������������������������������������������� 868
Understanding Statelessness and the Control Lifecycle �����������������������������������������������������������869
Using Session State ������������������������������������������������������������������������������������������������������������������������������������������� 870
Adding State through Form Elements ���������������������������������������������������������������������������������������������������������������� 872
Using View State ������������������������������������������������������������������������������������������������������������������������������������������������ 874
Using Control State �������������������������������������������������������������������������������������������������������������������������������������������� 876
Managing Application View State ����������������������������������������������������������������������������������������������879
Configuring Application View State �������������������������������������������������������������������������������������������������������������������� 882
Configuring Web Form and Control View State �������������������������������������������������������������������������������������������������� 885
Configuring Control View State �������������������������������������������������������������������������������������������������������������������������� 886
Putting It All Together ����������������������������������������������������������������������������������������������������������������888
Using View State for Input Elements ����������������������������������������������������������������������������������������������������������������� 891
Using View State in Child Controls ��������������������������������������������������������������������������������������������������������������������� 894
Adding to View State Data ��������������������������������������������������������������������������������������������������������������������������������� 896
Confusing View State and Control State ������������������������������������������������������������������������������������������������������������ 898
Summary �����������������������������������������������������������������������������������������������������������������������������������899
Chapter 33: Server-Side HTML Elements ■ �����������������������������������������������������������������������901
Preparing the Example Project ��������������������������������������������������������������������������������������������������901
Understanding Server-Side Elements ���������������������������������������������������������������������������������������901
Using the Base Class Features �������������������������������������������������������������������������������������������������������������������������� 902
Using Container Elements ��������������������������������������������������������������������������������������������������������������������������������� 904
Working with Page Structure Elements �������������������������������������������������������������������������������������907
■ Contents
xxix
Working with Form Elements ����������������������������������������������������������������������������������������������������909
Working with the input Element ������������������������������������������������������������������������������������������������������������������������ 909
Working with Other Form Elements ������������������������������������������������������������������������������������������������������������������� 917
Working with HTML Tables ��������������������������������������������������������������������������������������������������������919
Enumerating the Table ��������������������������������������������������������������������������������������������������������������������������������������� 920
Working with Specific Table Elements ��������������������������������������������������������������������������������������������������������������� 923
Creating Tables Programmatically ��������������������������������������������������������������������������������������������������������������������� 925
Working with Other Elements ����������������������������������������������������������������������������������������������������926
Putting It All Together ����������������������������������������������������������������������������������������������������������������927
Summary �����������������������������������������������������������������������������������������������������������������������������������929
Chapter 34: Model Binding ■ ��������������������������������������������������������������������������������������������931
Preparing the Example Project ��������������������������������������������������������������������������������������������������931
Understanding the Problem �������������������������������������������������������������������������������������������������������934
Applying Model Binding�������������������������������������������������������������������������������������������������������������936
Applying Model Validation Attributes ����������������������������������������������������������������������������������������������������������������� 938
Handling Model Binding and Validation Errors ��������������������������������������������������������������������������940
Using the Validation Summary ��������������������������������������������������������������������������������������������������������������������������� 944
Using Binding Attributes ������������������������������������������������������������������������������������������������������������947
Applying Model Binding Attributes �������������������������������������������������������������������������������������������������������������������� 951
Using Model Binding Attributes for Complex Types ������������������������������������������������������������������������������������������� 953
Putting It All Together ����������������������������������������������������������������������������������������������������������������955
Creating Self-Validating Model Classes ������������������������������������������������������������������������������������������������������������� 955
Creating Field-Level Error Controls ������������������������������������������������������������������������������������������������������������������� 956
Summary �����������������������������������������������������������������������������������������������������������������������������������959
Chapter 35: Data Binding ■ �����������������������������������������������������������������������������������������������961
Preparing the Example Project ��������������������������������������������������������������������������������������������������961
Understanding Data Binding������������������������������������������������������������������������������������������������������965
Configuring Data Binding ����������������������������������������������������������������������������������������������������������������������������������� 965
Combining Elements and Data Controls ������������������������������������������������������������������������������������������������������������ 970
■ Contents
xxx
Writing a Custom Data Control ��������������������������������������������������������������������������������������������������973
Managing Data Control View State �������������������������������������������������������������������������������������������������������������������� 977
Adding a Template to a Custom Data Control ���������������������������������������������������������������������������������������������������� 978
Adding Features to the Template ����������������������������������������������������������������������������������������������������������������������� 984
Putting It Together ���������������������������������������������������������������������������������������������������������������������986
Summary �����������������������������������������������������������������������������������������������������������������������������������990
Chapter 36: Basic Data Controls ■ ������������������������������������������������������������������������������������991
Selecting a Data Control ������������������������������������������������������������������������������������������������������������991
Preparing the Example Project ��������������������������������������������������������������������������������������������������992
Adding jQuery ���������������������������������������������������������������������������������������������������������������������������������������������������� 992
Using the List Data Controls ������������������������������������������������������������������������������������������������������993
Using the CheckBoxList Control ������������������������������������������������������������������������������������������������������������������������� 994
Using the DropDownList Control ���������������������������������������������������������������������������������������������������������������������� 1001
Using the ListBox Control �������������������������������������������������������������������������������������������������������������������������������� 1003
Using the RadioButtonList Control ������������������������������������������������������������������������������������������������������������������� 1006
Using the BulletedList Control ������������������������������������������������������������������������������������������������������������������������� 1008
Using the Repeater Control �����������������������������������������������������������������������������������������������������1010
Our Standard Repeater Usage ������������������������������������������������������������������������������������������������������������������������� 1010
Using the Repeater Templates ������������������������������������������������������������������������������������������������������������������������� 1012
Creating Templates Programmatically ������������������������������������������������������������������������������������������������������������� 1013
Putting It All Together ��������������������������������������������������������������������������������������������������������������1015
Summary ���������������������������������������������������������������������������������������������������������������������������������1019
Chapter 37: Complex Data Controls ■ �����������������������������������������������������������������������������1021
Preparing the Example Project ������������������������������������������������������������������������������������������������1021
Preparing Script Management ������������������������������������������������������������������������������������������������������������������������� 1021
Extending the CSS ������������������������������������������������������������������������������������������������������������������������������������������� 1022
Using the FormView Control ����������������������������������������������������������������������������������������������������1023
Defining the Code-Behind Class ���������������������������������������������������������������������������������������������������������������������� 1024
Defining the Templates ������������������������������������������������������������������������������������������������������������������������������������ 1025
Managing the Outer Element ��������������������������������������������������������������������������������������������������������������������������� 1028
■ Contents
xxxi
Editing Data with the FormView Control ���������������������������������������������������������������������������������������������������������� 1029
Implementing the Data Methods ��������������������������������������������������������������������������������������������������������������������� 1036
Understanding FormView Events ��������������������������������������������������������������������������������������������������������������������� 1039
Using the ListView Control ������������������������������������������������������������������������������������������������������1039
Using the Basic ListView Functionality ������������������������������������������������������������������������������������������������������������ 1040
Sorting Data with the ListView Control ������������������������������������������������������������������������������������������������������������ 1045
Paging Data ����������������������������������������������������������������������������������������������������������������������������������������������������� 1046
Understanding ListView Events ����������������������������������������������������������������������������������������������������������������������� 1048
Putting It All Together ��������������������������������������������������������������������������������������������������������������1049
Summary ���������������������������������������������������������������������������������������������������������������������������������1050
Chapter 38: Other ASP�NET Controls ■ ����������������������������������������������������������������������������1051
Preparing the Example Application �����������������������������������������������������������������������������������������1051
Working with the Rich UI Controls �������������������������������������������������������������������������������������������1051
Rich UI Controls Are Unnecessary Abstractions ����������������������������������������������������������������������������������������������� 1053
Rich UI Controls Modify Element Behavior ������������������������������������������������������������������������������������������������������ 1053
Rich UI Controls Rely on C# Events ������������������������������������������������������������������������������������������������������������������ 1054
Rich UI Controls Are Styled Directly ����������������������������������������������������������������������������������������������������������������� 1054
Selecting a Rich UI Control ������������������������������������������������������������������������������������������������������1055
Understanding Core Rich UI Control Features ������������������������������������������������������������������������������������������������� 1056
Using the Odds-and-Ends Controls �����������������������������������������������������������������������������������������1064
Using the Literal Control ���������������������������������������������������������������������������������������������������������������������������������� 1065
Using the PlaceHolder Control ������������������������������������������������������������������������������������������������������������������������� 1066
Using the MultiView Control ���������������������������������������������������������������������������������������������������������������������������� 1068
Putting It All Together ��������������������������������������������������������������������������������������������������������������1070
Summary ���������������������������������������������������������������������������������������������������������������������������������1072
Part 4: Client-Side Development ■ ������������������������������������������������������������� 1073
Chapter 39: Managing Scripts and Styles ■ �������������������������������������������������������������������1075
Preparing the Example Project ������������������������������������������������������������������������������������������������1075
Understanding Script Management Issues �����������������������������������������������������������������������������1077
■ Contents
xxxii
Managing JavaScript File Versioning ��������������������������������������������������������������������������������������������������������������� 1078
Managing Library Dependencies ��������������������������������������������������������������������������������������������������������������������� 1078
Managing Minification ������������������������������������������������������������������������������������������������������������������������������������� 1078
Using Bundles �������������������������������������������������������������������������������������������������������������������������1079
Preparing the Project for Bundles�������������������������������������������������������������������������������������������������������������������� 1079
Creating a Script Bundle ���������������������������������������������������������������������������������������������������������������������������������� 1080
Creating a Style Bundle ����������������������������������������������������������������������������������������������������������������������������������� 1084
Using Bundling Optimizations �������������������������������������������������������������������������������������������������1085
Using Local Optimization ��������������������������������������������������������������������������������������������������������������������������������� 1086
Using Content Delivery Networks �������������������������������������������������������������������������������������������������������������������� 1091
Ensuring Libraries Are Available for Controls ��������������������������������������������������������������������������1093
Putting It All Together ��������������������������������������������������������������������������������������������������������������1095
Summary ���������������������������������������������������������������������������������������������������������������������������������1097
Chapter 40: Ajax and Web Services ■ �����������������������������������������������������������������������������1099
Preparing the Example Project ������������������������������������������������������������������������������������������������1099
Creating Web Services Using Web API �������������������������������������������������������������������������������������1101
Understanding the Goal ����������������������������������������������������������������������������������������������������������������������������������� 1102
Creating the Web API Controller ����������������������������������������������������������������������������������������������������������������������� 1102
Creating the Routing Configuration ����������������������������������������������������������������������������������������������������������������� 1104
Testing the Web Service ���������������������������������������������������������������������������������������������������������������������������������� 1105
Implementing the Controller Methods ������������������������������������������������������������������������������������������������������������� 1108
Consuming the Web Service����������������������������������������������������������������������������������������������������1112
Dealing with Model Validation Errors ��������������������������������������������������������������������������������������������������������������� 1116
Dealing with Event Validation ��������������������������������������������������������������������������������������������������1117
Disabling Event Validation ������������������������������������������������������������������������������������������������������������������������������� 1119
Replacing the Control �������������������������������������������������������������������������������������������������������������������������������������� 1120
Putting It All Together ��������������������������������������������������������������������������������������������������������������1121
Summary ���������������������������������������������������������������������������������������������������������������������������������1124
■ Contents
xxxiii
Chapter 41: Client-Side Validation ■ �������������������������������������������������������������������������������1125
Preparing the Example Project ������������������������������������������������������������������������������������������������1125
Installing the JavaScript Packages ������������������������������������������������������������������������������������������������������������������ 1129
Creating the Validation Script Bundle �������������������������������������������������������������������������������������������������������������� 1129
Using HTML5 Validation �����������������������������������������������������������������������������������������������������������1130
Using the Built-In Validation Controls ��������������������������������������������������������������������������������������1133
Applying Validation Attributes Directly ������������������������������������������������������������������������������������1137
Defining the Validation Policy �������������������������������������������������������������������������������������������������������������������������� 1139
Removing Validation Policy Duplication ����������������������������������������������������������������������������������1141
Putting It All Together ��������������������������������������������������������������������������������������������������������������1145
Updating the Web Service ������������������������������������������������������������������������������������������������������������������������������� 1145
Updating the Model Object ������������������������������������������������������������������������������������������������������������������������������ 1145
Creating the JavaScript ����������������������������������������������������������������������������������������������������������������������������������� 1146
Summary ���������������������������������������������������������������������������������������������������������������������������������1148
Chapter 42: Targeting Mobile Devices ■ �������������������������������������������������������������������������1149
Preparing the Example Project ������������������������������������������������������������������������������������������������1149
Adding the jQuery Mobile Package ������������������������������������������������������������������������������������������������������������������ 1150
Identifying Mobile Devices ������������������������������������������������������������������������������������������������������1152
Switching Master Pages for Mobile Devices ���������������������������������������������������������������������������1154
Applying JavaScript Libraries via the Master Page ����������������������������������������������������������������������������������������� 1157
Delivering Different Web Forms ����������������������������������������������������������������������������������������������1158
Installing and Configuring the Package ����������������������������������������������������������������������������������������������������������� 1158
Delivering Custom Content ������������������������������������������������������������������������������������������������������������������������������ 1159
Allowing the User to Choose ���������������������������������������������������������������������������������������������������������������������������� 1160
Putting It All Together ��������������������������������������������������������������������������������������������������������������1162
Summary ���������������������������������������������������������������������������������������������������������������������������������1164
Index �������������������������������������������������������������������������������������������������������������������������������1165
xxxv
About the Author
Adam Freeman is an experienced IT professional who has held senior positions in a range of companies, most recently serving as chief technology officer and chief operating officer of a global bank. Now retired, he spends his time writing and running.
xxxvii
About the Technical Reviewer
Fabio Claudio Ferracchiati is a senior consultant and a senior analyst/developer using Microsoft technologies. He works for Brain Force (http://www.brainforce.com) in its Italian branch (http://www.brainforce.it). He is a Microsoft Certified Solution Developer for .NET, a Microsoft Certified Application Developer for .NET, a Microsoft Certified Professional, and a prolific author and technical reviewer. Over the past 10 years, he’s written articles for Italian and international magazines and coauthored more than 10 books on a variety of computer topics.
xxxix
Acknowledgments
I would like to thank everyone at Apress for working so hard to bring this book to print. In particular, I would like to thank Ewan Buckingham for commissioning and editing this title and Christine Ricketts for keeping track of everything. I would also like to thank the technical reviewer, Fabio, whose efforts made this book far better than it otherwise would have been.
—Adam Freeman