Third Edition Programming - Pearsoncmg

3y ago
34 Views
3 Downloads
998.00 KB
71 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Averie Goad
Transcription

Programming Microsoft ASP.NET MVCThird EditionGo deep into the architecture and features of ASP.NET MVC 5,and learn how to build web applications that work well on boththe desktop and mobile devices. Web development expert DinoEsposito takes you through the web framework’s Model-ViewController (MVC) design model, and covers the tools you needto cleanly separate business logic from the user interface. Ifyou’re an experienced web developer new to ASP.NET MVC, thispractical guide will get you going.About the AuthorDino Esposito is a well-known weband mobile development expert. Hespeaks at industry events, includingDevConnections and Microsoft TechEd,and contributes to MSDN Magazineand other publications. He has writtenseveral popular Microsoft Press books,including Architecting Mobile Solutionsfor the Enterprise.Discover how to: Build web applications that are easy to test and maintain Dive into the functions of controllers—the heart of an MVC site Explore the structure and behavior of a view engine Process a variety of input data using a custom model binder Automate the writing of input forms, and streamline validation Design websites for mobile devices, localization, and errorhandlingProgramming MicrosoftASP.NET MVCDevelop next-generation web applications withASP.NET MVCProgrammingMicrosoftASP.NET MVCThird Edition Provide security by implementing a membership and identity3(WURFL) to make sites mobile-friendlyDownload code samples at:editionhttp://aka.ms/programASP-NET MVC/filesEspositomicrosoft.com/mspressU.S.A. 49.99Canada 52.99Professionalsystem Inject script code into your site using JavaScript and jQuery Use Responsive Web Design and Wireless Universal Resource FiLe[Recommended]Programming/Microsoft ASP.NET MVCCelebrating 30 years!Dino Esposito

Programming MicrosoftASP.NET MVC, ThirdEditionDino Espositomvc5 book.indb 12/6/14 1:55 PM

Copyright 2014 Leonardo EspositoAll rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by anymeans without the written permission of the publisher.ISBN: 978-0-7356-8094-4Second Printing: April 2014Printed and bound in the United States of America.Microsoft Press books are available through booksellers and distributors worldwide. If you need support relatedto this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think ofthis book at soft and the trademarks listed at ctualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property oftheir respective owners.The example companies, organizations, products, domain names, email addresses, logos, people, places, andevents depicted herein are fictitious. No association with any real company, organization, product, domain name,email 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 withoutany express, statutory, or implied warranties. Neither the author, O’Reilly Media, Inc., Microsoft Corporation, norits resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly orindirectly by this book.Acquisitions and Developmental Editors: Russell Jones and Rachel RoumeliotisProduction Editor: Kristen BrownEditorial Production: Dianne Russell, Octal Publishing, Inc.Technical Reviewer: John MuellerCopyeditor: Bob Russell, Octal Publishing, Inc.Indexer: BIM Indexing ServicesCover Design: Twist Creative Seattle and Joel PanchotCover Composition: Ellie VolckhausenIllustrator: Rebecca Demarest

To Silvia, Francesco, Michela, and my back for sustaining me.—Dinomvc5 book.indb 32/6/14 1:55 PM

mvc5 book.indb 42/6/14 1:55 PM

mvc5 book.indb 62/6/14 1:55 PM

ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiPart IASP.NET MVC FUNDAMENTALSChapter 1ASP.NET MVC controllers3Routing incoming requests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Simulating the ASP.NET MVC runtime. . . . . . . . . . . . . . . . . . . . . . . . . . 4The URL routing HTTP module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Application routes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9The controller class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Aspects of a controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Writing controller classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Processing input data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Producing action results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30Chapter 2ASP.NET MVC views33The structure and behavior of a view engine . . . . . . . . . . . . . . . . . . . . . . . . 34The mechanics of a view engine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Definition of the view template. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39HTML helpers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Basic helpers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Templated helpers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Custom helpers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51The Razor view engine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Inside the view engine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Designing a sample view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Coding the view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Modeling the view. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Advanced features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73viimvc5 book.indb 72/6/14 1:55 PM

Chapter 3The model-binding architecture75The input model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Evolving from the Web Forms input processing . . . . . . . . . . . . . . . . 76Input processing in ASP.NET MVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Model binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Model-binding infrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79The default model binder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Customizable aspects of the default binder. . . . . . . . . . . . . . . . . . . . 91Advanced model binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Custom type binders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93A sample DateTime model binder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101Chapter 4Input forms103General patterns of data entry. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104A classic Select-Edit-Post scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Applying the Post-Redirect-Get pattern. . . . . . . . . . . . . . . . . . . . . . 112Automating the writing of input forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Predefined display and editor templates. . . . . . . . . . . . . . . . . . . . . . 117Custom templates for model data types. . . . . . . . . . . . . . . . . . . . . . 125Input validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Using data annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Advanced data annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135Self-validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146Part IIASP.NET MVC SOFTWARE DESIGNChapter 5Aspects of ASP.NET MVC applications151ASP.NET intrinsic objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151HTTP response and SEO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Managing the session state. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Caching data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157viiimvc5 book.indb 8Contents2/6/14 1:55 PM

Error handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Handling program exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Global error handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Dealing with missing content. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Using localizable resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Dealing with localizable applications. . . . . . . . . . . . . . . . . . . . . . . . . 182Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187Chapter 6Securing your application189Security in ASP.NET MVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Authentication and authorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Separating authentication from authorization. . . . . . . . . . . . . . . . . 192Implementing a membership system. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Defining a membership controller. . . . . . . . . . . . . . . . . . . . . . . . . . . 196The Remember-Me feature and Ajax. . . . . . . . . . . . . . . . . . . . . . . . . 204External authentication services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207The OpenID protocol. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Authenticating via social networks. . . . . . . . . . . . . . . . . . . . . . . . . . . 215Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223Chapter 7Design considerations for ASP.NET MVC controllers 225Shaping up your controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226Choosing the right stereotype. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226Fat-free controllers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Connecting the presentation and back end. . . . . . . . . . . . . . . . . . . . . . . . .237The Layered Architecture pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . 237Injecting data and services in layers. . . . . . . . . . . . . . . . . . . . . . . . . . 244Gaining control of the controller factory. . . . . . . . . . . . . . . . . . . . . . 250Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253ixmvc5 book.indb 92/6/14 1:55 PM

Chapter 8Customizing ASP.NET MVC controllers255The extensibility model of ASP.NET MVC. . . . . . . . . . . . . . . . . . . . . . . . . . . 255The provider-based model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256The Service Locator pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Adding aspects to controllers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263Action filters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263Gallery of action filters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267Special filters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274Building a dynamic loader filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Action result types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285Built-in action result types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285Custom result types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299Chapter 9Testing and testability in ASP.NET MVC301Testability and design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302DfT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302Loosen up your design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304The basics of unit testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308Workin

ASP.NET MVC Develop next-generation web applications with ASP.NET MVC Go deep into the architecture and features of ASP.NET MVC 5, and learn how to build web applications that work well on both the desktop and mobile devices. Web development expert Dino Esposito takes you through the web framework’s Model-View-

Related Documents:

RP 2K, Second Edition RP 2L, Third Edition RP 2M, First Edition Bul 2N, First Edition RP 2P, Second Edition RP 2Q, Second Edition RP 2R, First Edition RP 2T, First Edition Bul 2U, First Edition Bul 2V, First Edition Spec 2W, First Edition RP 2X, First Edition, with Supp 1 Spec 2Y, First Edition

Saxon Math 6/5 Third Edition Saxon Math 7/6 Fourth Edition Saxon Math 8/7 Third Edition Algebra 1/2, An Incremental Development, Third Edition Algebra I, An Incremental Development, Third Edition Please call us toll-free at 1-866-492-6284 to order this Accelerated Math library or to request the Scope and Sequence for other libraries .

Fifth Edition 1977–1978 Sixth Edition 1979–1980 Seventh Edition 1981–1982 Eighth Edition 1983–1986 Ninth Edition 1987–1988 Tenth Edition 1989–1990 Eleventh Edition 1991–1992 Twelfth Edition 1993–1994 Thirteenth Edition 1995–1996 Fourteenth Edition 1997–1998 Fifteenth Edition

BIOS INSTANT NOTES Series Editor: B.D. Hames, School of Biochemistry and Microbiology, University of Leeds, Leeds, UK Biology Animal Biology, Second Edition Biochemistry, Third Edition Bioinformatics Chemistry for Biologists, Second Edition Developmental Biology Ecology, Second Edition Genetics, Second Edition Immunology, Second Edition

The AP Calculus Problem Book Publication history: First edition, 2002 Second edition, 2003 Third edition, 2004 Third edition Revised and Corrected, 2005 Fourth edition, 2006, Edited by Amy Lanchester Fourth edition Revised and Corrected, 2007 Fourth edition, Corrected, 2008 This

Object Oriented Programming 7 Purpose of the CoursePurpose of the Course To introduce several programming paradigms including Object-Oriented Programming, Generic Programming, Design Patterns To show how to use these programming schemes with the C programming language to build “good” programs.

Functional programming paradigm History Features and concepts Examples: Lisp ML 3 UMBC Functional Programming The Functional Programming Paradigm is one of the major programming paradigms. FP is a type of declarative programming paradigm Also known as applicative programming and value-oriented

1 1 Programming Paradigms ØImperative Programming – Fortran, C, Pascal ØFunctional Programming – Lisp ØObject Oriented Programming – Simula, C , Smalltalk ØLogic Programming - Prolog 2 Parallel Programming A misconception occurs that parallel