Professional Asp.Net Mvc 3
Total Page:16
File Type:pdf, Size:1020Kb
ffirs.indd ii 7/4/2011 4:27:38 PM Download from Wow! eBook <www.wowebook.com> ffirs.indd i i INTRODUCTION . INTRODUCTION FOREWORD. 3 MVC ASP.NET PROFESSIONAL INDEX . INDEX CHAPTER 14 CHAPTER 13 CHAPTER 12 CHAPTER 11 CHAPTER 10 CHAPTER 9 CHAPTER 8 CHAPTER 7 CHAPTER 6 CHAPTER 5 CHAPTER 4 CHAPTER 3 CHAPTER 2 CHAPTER 1 Advanced Topics . 339 . Extending315 MVC . Unit Testing 291 . Dependency Injection 271 . .239NuGet . .211Routing AJAX. 179 ApplicationSecuring Your . 135 Data Annotations and Validation . 117 . 93 Forms and HTML Helpers Models. 69 Views . 39 . 23 Controllers Getting. .1 Started 389 xxiii xxv 7/4/2011 4:27:37 PM 4:27:37 PM ffirs.indd ii 7/4/2011 4:27:38 PM PROFESSIONAL ASP.NET MVC 3 Jon Galloway Phil Haack Brad Wilson K. Scott Allen ffirs.indd iii 7/4/2011 4:27:38 PM Professional ASP.NET MVC 3 Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2011 by John Wiley & Sons, Inc. Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-118-07658-3 ISBN: 978-1-118-15535-6 (ebk) ISBN: 978-1-118-15537-0 (ebk) ISBN: 978-1-118-15536-3 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748- 6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or pro- motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the pub- lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2011930287 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade- marks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. ffirs.indd iv 7/4/2011 4:27:43 PM To my wife Rachel, my daughters Rosemary, Esther, and Ellie, and to you for reading this book. Enjoy! — Jon Galloway My wife, Akumi, deserves to have her name on the cover as much as I do for all her support made this possible. And thanks to Cody for his infectious happiness. — Phil Haack To Potten on Potomac. — K. Scott Allen ffirs.indd v 7/4/2011 4:27:43 PM CREDITS ACQUISITIONS EDITOR PRODUCTION MANAGER Paul Reese Tim Tate PROJECT EDITOR VICE PRESIDENT AND EXECUTIVE GROUP PUBLISHER Maureen Spears Richard Swadley TECHNICAL EDITORS VICE PRESIDENT AND EXECUTIVE PUBLISHER Eilon Lipton Neil Edde PRODUCTION EDITOR ASSOCIATE PUBLISHER Daniel Scribner Jim Minatel COPY EDITOR PROJECT COORDINATOR, COVER Kimberly A. Cofer Katherine Crocker EDITORIAL MANAGER PROOFREADER Mary Beth Wakefi eld Sheilah Ledwidge, Word One FREELANCER EDITORIAL MANAGER INDEXER Rosemarie Graham Robert Swanson ASSOCIATE DIRECTOR OF MARKETING COVER DESIGNER David Mayhew LeAndra Young BUSINESS MANAGER COVER IMAGE Any Knies © Getty / David Madison ffirs.indd vi 7/4/2011 4:27:43 PM ABOUT THE AUTHORS JON GALLOWAY works at Microsoft as a Community Program Manager focused on ASP.NET MVC. He wrote the MVC Music Store tutorial, helped organize mvcConf (a free online conference for the ASP.NET MVC community), and travelled the world in 2010 teaching MVC classes for the Web Camps tour. Jon previously worked at Vertigo Software, where he worked on several Microsoft conference websites, high profi le Silverlight video players, and MIX keynote demos. Prior to that, he’s worked in a wide range of web development shops, from scrappy startups to Fortune 500 fi nancial companies. He’s part of the Herding Code podcast (http://herdingcode.com), blogs at http://weblogs.asp.net/jgalloway, and twitters as @jongalloway. He lives in San Diego with his wife, three daughters, and a bunch of avocado trees. PHIL HAACK is a Senior Program Manager with the ASP.NET team working on the ASP.NET MVC project. Prior to joining Microsoft, Phil worked as a product manager for a code search engine, a dev manager for an online gaming company, and as a senior architect for a popular Spanish lan- guage television network, among other crazy pursuits. As a code junkie, Phil Haack loves to craft software. Not only does he enjoy writing software, he enjoys writing about software and software management on his blog, http://haacked.com/. In his spare time, Phil contributes to various open source projects and is the founder of the Subtext blog engine project, which is undergoing a re-write, using ASP.NET MVC, of course. BRAD WILSON works for Microsoft as a Senior Software Developer on the Web Platform and Tools team on the ASP.NET MVC project. He joined Microsoft on the Patterns and Practices team in 2005, and also worked on the team that builds the CodePlex open source hosting site. Prior to Microsoft, he has been a developer, consultant, architect, team lead, and CTO at various software companies for nearly 20 years. He’s also the co-author of the xUnit.net open source developer test- ing framework, along with James Newkirk (of NUnit fame). He has been an active blogger since 2001 and writes primarily on ASP.NET topics at http://bradwilson.typepad.com/ as well as tweeting as @bradwilson. Brad lives in beautiful Redmond, WA, where he hones his love for all types of games — especially Poker. K. SCOTT ALLEN is the founder of OdeToCode LLC. Scott provides custom development, consulting, and mentoring services for clients around the world. ffirs.indd vii 7/4/2011 4:27:43 PM ABOUT THE TECHNICAL EDITORS EILON LIPTON joined the ASP.NET team as a developer at Microsoft in 2002. On this team, he has worked on areas ranging from data source controls to localization to the UpdatePanel control. He now works on the ASP.NET MVC Framework as a principal development lead. Eilon is also a fre- quent speaker on a variety of ASP.NET-related topics at conferences worldwide. He graduated from Boston University with a dual degree in Math and Computer Science. In his spare time Eilon spends time in his garage workshop building what he considers to be well-designed furniture. If you know anyone who needs a coffee table that’s three feet tall and has a slight slope to it, send him an e-mail. ffirs.indd viii 7/4/2011 4:27:43 PM ACKNOWLEDGMENTS THANKS TO FAMILY AND FRIENDS who graciously acted as if “Jon without sleep” is someone you’d want to spend time with. Thanks to the whole ASP.NET team for making work fun since 2002, and especially to Brad Wilson and Phil Haack for answering tons of random questions. Thanks to Warren G. Harding for normalcy. Thanks to Philippians 4:4-9 for continually reminding me which way is up. — Jon Galloway THANKS GO TO MY LOVELY WIFE, Akumi, for her support which went above and beyond all expecta- tions and made this possible. I’d like to also give a shout out to my son, Cody, for his sage advice, delivered only as a two year old can deliver it. I’m sure he’ll be embarrassed ten years from now that I used such an anachronism (“shout out”) in my acknowledgment to him. Thanks go to my daughter, Mia, as her smile lights up the room like unicorns. — Phil Haack ffirs.indd ix 7/4/2011 4:27:44 PM Download from Wow! eBook <www.wowebook.com> ffirs.indd x x 7/4/2011 4:27:44 PM 4:27:44 PM CONTENTS FOREWORD xxiii INTRODUCTION xxv CHAPTER 1: GETTING STARTED 1 A Quick Introduction to ASP.NET MVC 1 How ASP.NET MVC Fits in with ASP.NET 2 The MVC Pattern 2 MVC as Applied to Web Frameworks 3 The Road to MVC 3 3 ASP.NET MVC