www.it-ebooks.info PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2010 by Dino Esposito All rights reserved No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher Library of Congress Control Number: 2010925900 Printed and bound in the United States of America WCT Distributed in Canada by H.B Fenn and Company Ltd A CIP catalogue record for this book is available from the British Library Microsoft Press books are available through booksellers and distributors worldwide For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329 Visit our Web site at www.microsoft.com/mspress Send comments to mspinput@microsoft.com Microsoft, Microsoft Press, ActiveX, Excel, IntelliSense, Internet Explorer, MS, MSDN, SharePoint, Silverlight, SQL Server, Visual Basic, Visual C#, Visual Studio, Win32, Windows, Windows Server, and Windows Vista Other product and company names mentioned herein may be the trademarks of their respective owners The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred This book expresses the author’s views and opinions The information contained in this book is provided without any express, statutory, or implied warranties Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book Acquisitions Editor: Ben Ryan Developmental Editor: Lynn Finnel Project Editors: Lynn Finnel and Carol Vu Editorial Production: Ashley Schneider, S4Carlisle Publishing Services Technical Reviewer: Kenn Scribner; Technical Review services provided by Content Master, a member of CM Group, Ltd Cover: Tom Draper Design Body Part No X16-88503 www.it-ebooks.info To Silvia, Francesco, and Michela, who wait for me and keep me busy But I’m happy only when I’m busy —Dino www.it-ebooks.info www.it-ebooks.info Contents at a Glance Part I Goals of ASP.NET MVC and Motivation for Its Development The Runtime Environment 37 The MVC Pattern and Beyond 81 Part II The Programming Paradigm The Core of ASP.NET MVC Inside Controllers 123 Inside Views 211 Inside Models 277 Part III Programming Features Data Entry in ASP.NET MVC 317 The ASP.NET MVC Infrastructure 355 AJAX Capabilities 401 10 Testability and Unit Testing 435 11 Customizing ASP.NET MVC 477 www.it-ebooks.info v www.it-ebooks.info Table of Contents Acknowledgments xiii Introduction xv Part I The Programming Paradigm Goals of ASP.NET MVC and Motivation for Its Development The Deep Impact of ASP.NET Productivity Is King The Web Forms Model The “Page Controller” Pattern 11 The ASP.NET Age of Reason 16 ASP.NET’s Signs of Aging 16 The Turning Point 20 ASP.NET MVC at a Glance 26 ASP.NET MVC Highlights 26 Web Forms vs ASP.NET MVC 30 Summary 35 The Runtime Environment 37 The ASP.NET Runtime Machinery 37 ASP.NET and the IIS Web Server 38 Life Cycle of an ASP.NET Request 44 What’s an HTTP Handler, Anyway? 51 What’s an HTTP Module, Anyway? 57 URL Routing 61 The ASP.NET MVC Run-Time Shell 67 The Big Picture 68 Processing an ASP.NET MVC Request 75 Summary 80 What you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you To participate in a brief online survey, please visit: www.microsoft.com/learning/booksurvey/ www.it-ebooks.info vii viii Table of Contents The MVC Pattern and Beyond 81 The Original MVC Pattern 82 MVC Interaction Model 82 The Original Idea 83 Presenting the Actors 84 Limitations of the MVC Pattern 89 The Model2 Pattern 90 MVC and the Web 90 Model2 and ASP.NET MVC 93 Presentation-Oriented Variations of MVC 98 The MVP Pattern 98 Presentation Model Pattern (Also Known as MVVM) 103 The ASP.NET MVC Project Template 107 Peculiarities of an ASP.NET MVC Project 108 ASP.NET MVC Special Folders 113 Summary 118 Part II The Core of ASP.NET MVC Inside Controllers 123 The Role of Controllers and the Motivation for Using Them 123 Beyond the Code-Behind Approach 124 Introducing Controllers 128 Mechanics of Controllers in ASP.NET MVC 132 Anatomy of an ASP.NET MVC Controller 135 Inside the Structure of a Controller 135 Behavior of a Controller 144 Attributes of Controllers and Action Methods 156 Writing a Controller 167 Design of a Controller Class 167 Should You Use Your Own Base Class? 176 Special Capabilities 183 Grouping Controllers 184 Asynchronous Controllers 187 Render Actions 195 Controllers and Testability 199 Making Controllers Easy to Test 199 Writing Unit Tests 204 Summary 209 www.it-ebooks.info Table of Contents Inside Views 211 Views and Controllers 212 From Controllers to Views 212 Building the Response for the Browser 213 Anatomy of an ASP.NET MVC View 215 Selecting the View 215 Creating the View 220 The Default View Engine 222 The Web Forms View Engine 226 Writing a View 233 The View’s Template 235 Filling Up the View 241 HTML Helpers 252 Templated HTML Helpers 257 Datagrids and Paged Views 261 Testing a View 273 Summary 275 Inside Models 277 What’s the Model, Anyway? 278 How Many Types of Models Do You Know? 278 The Models Folder 282 Domain Model and View-Model 286 Business Object Modeling 286 Adding Validation Logic to the Model 291 Data for the View 299 Model Binding 305 The Model Binder in Action 306 Summary 313 Part III Programming Features Data Entry in ASP.NET MVC 317 The Select-Edit-Save Pattern 318 Presenting Data 318 Editing Data 320 Saving Data 324 Data Validation 326 Validation on the Server Side 326 www.it-ebooks.info ix x Table of Contents Giving Feedback to the User 334 Data Annotations and Validators 342 Client-Side Validation 351 Summary 354 The ASP.NET MVC Infrastructure 355 Routing 356 Dealing with Routes 356 Keeping an Eye on SEO 362 Error Handling 366 Foundations of ASP.NET Error Handling 366 Dealing with Missing Content 371 Localization 374 Making Resources Localizable 374 Dealing with Resources in ASP.NET MVC 376 Dependency Injection 382 Dependency Inversion in Action 383 A Brief Tour of Unity 391 Creating a Global Container 395 Summary 399 AJAX Capabilities 401 AJAX in ASP.NET 401 Partial Rendering 402 Direct Scripting 405 AJAX in ASP.NET MVC 408 The JavaScript API 408 The Controller Faỗade 413 AJAX Helpers in ASP.NET MVC 420 Partial Rendering in ASP.NET MVC 428 Summary 432 10 Testability and Unit Testing 435 Testability and Design 436 Design for Testability 436 Loosen Up Your Design 438 Basics of Unit Testing 443 Working with a Test Harness 443 Aspects of Testing 447 www.it-ebooks.info ... valid) at http://www.dotnetslackers.com/articles/aspnet/AnArchitecturalViewOfTheASPNETMVCFramework aspx The taste of ASP. NET MVC was bittersweet for me Overall, ASP. NET MVC seemed like an entire... of them (ASP. NET MVC) can help you write better code, faster This may not be the case today with ASP. NET MVC 2, but it likely will be the case with ASP. NET MVC or I don’t think that ASP. NET Web... just-released, first preview of ASP. NET MVC ASP. NET what? I had a look at the bits, and a few weeks later I even wrote one of the very first articles about ASP. NET MVC for the DotNetSlackers Web site The