CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN .NET Companion eBook Available Pro ASP.NET MVC 2 Framework Pro Microsoft’s ASP.NET MVC Framework has dramatically shifted .NET web devel- A opment into the modern age. It promotes maintainability through clean archi- tecture and separation of concerns, tight control over HTML and URLs, unit testability, powerful extensibility, and easy integration with third-party libraries S such as jQuery. Now, based on real-world feedback, version 2 of the framework Pro adds many valuable enhancements for security, scalability, and simplifying data Steven Sanderson, entry and validation. P Author of The original edition of this book was the highest rated and best selling of all Pro ASP.NET MVC books on ASP.NET MVC, so I was excited by the chance to update it and build on . Framework ASP.NET MVC 2 that success. My hope is that this new edition will give you the deepest understand- N ing of everything that ASP.NET MVC 2 offers. You’ll find major new sections about the framework’s new version 2 features, and the whole book is thoroughly revised and expanded to account for .NET 4 and the latest best practices. You’ll learn about: E • The MVC Framework’s powerful facilities, including routing, controllers, filters, T views, model metadata, model binding, and validation Framework • Architecture, including the model-view-controller (MVC) pattern, test-driven development (TDD), behavior-driven development (BDD), and relevant design patterns such as dependency injection M • Extending and customizing the MVC Framework’s request processing pipeline • Securing your MVC application and deploying it to Windows Server • Upgrading from ASP.NET MVC 1, and integrating with or upgrading from traditional ASP.NET (also known as Web Forms) V This book does not assume that you have any existing knowledge of ASP.NET MVC. It assumes only that you have a working knowledge of C# and some web C development experience. Enjoy, Build the most maintainable, standards-compliant, and best Steven Sanderson performing web applications on the Microsoft platform Companion eBook 2 THE APRESS ROADMAP Pro Pro C# 2010 ASP.NET MVC 2 Introducing and the SECOND EDITION .NET 4.0 .NET 4 Platform SECOND See last page for details Pro EDITION on $10 eBook version Silverlight 4 in C# Accelerated Pro ASP.NET 4 C# 2010 in C# 2010 Pro LINQ S a n Steven Sanderson SOURCE CODE ONLINE d www.apress.com ISBN 978-1-4302-2886-8 e r 55499 s o n US $54.99 Shelve in: .NET User level: 9 781430 228868 Intermediate–Advanced this print for content only—size & color not accurate 7.5 x 9.5 spine =1.46875 776 page count Pro ASP.NET MVC 2 Framework ■ ■ ■ Steven Sanderson i Pro ASP.NET MVC 2 Framework Copyright © 2010 by Steven Sanderson All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2886-8 ISBN-13 (electronic): 978-1-4302-2887-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 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. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Main Technical Reviewer: Stefan Turalski Additional Technical Reviewers: Jimmy Skowronski, Bryan Avery Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett Copy Editor: Damon Larson Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. 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/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. ii To Zoe, who once again loved and supported me throughout this project iii Contents at a Glance ■Contents at a Glance........................................................................................................iv(cid:1) ■Contents............................................................................................................................v(cid:1) ■About the Author.............................................................................................................xx(cid:1) ■About the Technical Reviewers......................................................................................xxi(cid:1) ■Acknowledgments.........................................................................................................xxii(cid:1) ■Introduction..................................................................................................................xxiii Part 1: Introducing ASP.NET MVC 2.....................................................................................1(cid:1) ■Chapter 1: What’s the Big Idea?.......................................................................................3(cid:1) ■Chapter 2: Your First ASP.NET MVC Application.............................................................15(cid:1) ■Chapter 3: Prerequisites.................................................................................................43(cid:1) ■Chapter 4: SportsStore: A Real Application....................................................................91(cid:1) ■Chapter 5: SportsStore: Navigation and Shopping Cart................................................135(cid:1) ■Chapter 6: SportsStore: Administration and Final Enhancements...............................179(cid:1) Part 2: ASP.NET MVC in Detail.........................................................................................213(cid:1) ■Chapter 7: Overview of ASP.NET MVC Projects..............................................................215(cid:1) ■Chapter 8: URLs and Routing........................................................................................235(cid:1) ■Chapter 9: Controllers and Actions...............................................................................283(cid:1) ■Chapter 10: Controller Extensibility..............................................................................325(cid:1) ■Chapter 11: Views.........................................................................................................373(cid:1) ■Chapter 12: Models and Data Entry..............................................................................409(cid:1) ■Chapter 13: User Interface Techniques........................................................................477(cid:1) ■Chapter 14: Ajax and Client Scripting...........................................................................517(cid:1) Part 3: Delivering Successful ASP.NET MVC 2 Projects...................................................561(cid:1) ■Chapter 15: Security and Vulnerability.........................................................................563(cid:1) ■Chapter 16: Deployment...............................................................................................585(cid:1) ■Chapter 17: ASP.NET Platform Features.......................................................................619(cid:1) ■Chapter 18: Upgrading and Combining ASP.NET Technologies....................................675(cid:1) ■Index.............................................................................................................................701 iv Contents ■Contents at a Glance............................................................................................iv(cid:1) ■Contents................................................................................................................v(cid:1) ■About the Author.................................................................................................xx(cid:1) ■About the Technical Reviewers..........................................................................xxi(cid:1) ■Acknowledgments.............................................................................................xxii(cid:1) ■Introduction......................................................................................................xxiii Part 1: Introducing ASP.NET MVC 2..........................................................................1(cid:1) ■Chapter 1: What’s the Big Idea?............................................................................3(cid:1) A Brief History of Web Development...............................................................................3(cid:1) Traditional ASP.NET Web Forms............................................................................................................4(cid:1) What’s Wrong with ASP.NET Web Forms?.............................................................................................5(cid:1) Web Development Today................................................................................................6(cid:1) Web Standards and REST......................................................................................................................6(cid:1) Agile and Test-Driven Development.......................................................................................................7(cid:1) Ruby on Rails.........................................................................................................................................7(cid:1) Key Benefits of ASP.NET MVC.........................................................................................8(cid:1) MVC Architecture...................................................................................................................................8(cid:1) Extensibility............................................................................................................................................8(cid:1) Tight Control over HTML and HTTP........................................................................................................9(cid:1) Testability...............................................................................................................................................9(cid:1) Powerful Routing System.....................................................................................................................10(cid:1) Built on the Best Parts of the ASP.NET Platform..................................................................................10 v ■ CONTENTS Modern API...........................................................................................................................................11(cid:1) ASP.NET MVC Is Open Source..............................................................................................................11(cid:1) Who Should Use ASP.NET MVC?...................................................................................11(cid:1) Comparisons with ASP.NET Web Forms..............................................................................................11(cid:1) Comparisons with Ruby on Rails.........................................................................................................12(cid:1) Comparisons with MonoRail................................................................................................................13(cid:1) What’s New in ASP.NET MVC 2.....................................................................................13(cid:1) Summary.......................................................................................................................14(cid:1) ■Chapter 2: Your First ASP.NET MVC Application.................................................15(cid:1) Preparing Your Workstation..........................................................................................15(cid:1) Creating a New ASP.NET MVC Project..........................................................................16(cid:1) Adding the First Controller...................................................................................................................18(cid:1) How Does It Know to Invoke HomeController?.....................................................................................19(cid:1) Rendering Web Pages...................................................................................................19(cid:1) Creating and Rendering a View............................................................................................................19(cid:1) Adding Dynamic Output.......................................................................................................................22(cid:1) A Starter Application.....................................................................................................23(cid:1) The Story..............................................................................................................................................23(cid:1) Designing a Data Model.......................................................................................................................24(cid:1) Linking Between Actions......................................................................................................................25(cid:1) Building a Form....................................................................................................................................29(cid:1) Handling Form Submissions................................................................................................................32(cid:1) Adding Validation.................................................................................................................................35(cid:1) Finishing Off.........................................................................................................................................39(cid:1) Summary.......................................................................................................................41(cid:1) ■Chapter 3: Prerequisites.....................................................................................43(cid:1) Understanding MVC Architecture..................................................................................43(cid:1) The Smart UI (Anti-Pattern)..................................................................................................................44(cid:1) Separating Out the Domain Model.......................................................................................................45(cid:1) vi ■ CONTENTS Three-Tier Architecture........................................................................................................................46(cid:1) MVC Architecture.................................................................................................................................47(cid:1) Variations on MVC................................................................................................................................49(cid:1) Domain Modeling..........................................................................................................50(cid:1) An Example Domain Model..................................................................................................................51(cid:1) Ubiquitous Language...........................................................................................................................52(cid:1) Aggregates and Simplification.............................................................................................................52(cid:1) Keeping Data Access Code in Repositories..........................................................................................54(cid:1) Using LINQ to SQL................................................................................................................................55(cid:1) Building Loosely Coupled Components.........................................................................61(cid:1) Taking a Balanced Approach...............................................................................................................62(cid:1) Using Dependency Injection.................................................................................................................62(cid:1) Using a DI Container.............................................................................................................................64(cid:1) Getting Started with Automated Testing.......................................................................66(cid:1) Understanding Unit Testing..................................................................................................................67(cid:1) Understanding Integration Testing.......................................................................................................73(cid:1) C# 3 Language Features...............................................................................................78(cid:1) The Design Goal: Language-Integrated Query.....................................................................................78(cid:1) Extension Methods...............................................................................................................................79(cid:1) Lambda Methods.................................................................................................................................80(cid:1) Generic Type Inference........................................................................................................................81(cid:1) Automatic Properties...........................................................................................................................81(cid:1) Object and Collection Initializers..........................................................................................................82(cid:1) Type Inference.....................................................................................................................................82(cid:1) Anonymous Types................................................................................................................................83(cid:1) Using LINQ to Objects...........................................................................................................................85(cid:1) Lambda Expressions............................................................................................................................86(cid:1) IQueryable<T> and LINQ to SQL..........................................................................................................87(cid:1) Summary.......................................................................................................................89(cid:1) ■Chapter 4: SportsStore: A Real Application.........................................................91(cid:1) vii ■ CONTENTS Getting Started..............................................................................................................93(cid:1) Creating Your Solutions and Projects...................................................................................................93(cid:1) Starting Your Domain Model.........................................................................................96(cid:1) Creating an Abstract Repository..........................................................................................................97(cid:1) Making a Fake Repository....................................................................................................................98(cid:1) Displaying a List of Products........................................................................................98(cid:1) Adding the First Controller...................................................................................................................99(cid:1) Setting Up the Default Route..............................................................................................................100(cid:1) Adding the First View.........................................................................................................................101(cid:1) Connecting to a Database...........................................................................................104(cid:1) Defining the Database Schema..........................................................................................................104(cid:1) Setting Up LINQ to SQL.......................................................................................................................107(cid:1) Creating a Real Repository.................................................................................................................107(cid:1) Setting Up DI...............................................................................................................109(cid:1) Creating a Custom Controller Factory................................................................................................109(cid:1) Using Your DI Container.....................................................................................................................110(cid:1) Creating Unit Tests......................................................................................................113(cid:1) Configuring a Custom URL Schema............................................................................118(cid:1) Assigning a Default Parameter Value.................................................................................................119(cid:1) Displaying Page Links........................................................................................................................120(cid:1) Improving the URLs............................................................................................................................128(cid:1) Styling It Up.................................................................................................................129(cid:1) Defining Page Layout in the Master Page..........................................................................................129(cid:1) Adding CSS Rules...............................................................................................................................130(cid:1) Creating a Partial View.......................................................................................................................132(cid:1) Summary.....................................................................................................................134(cid:1) ■Chapter 5: SportsStore: Navigation and Shopping Cart....................................135(cid:1) Adding Navigation Controls.........................................................................................135(cid:1) Filtering the Product List....................................................................................................................135(cid:1) viii
Description: