Develop cloud-ready web applications using Microsofts latest framework, ASP.NET Core MVC 2 Seventh Edition... Contents at a Glance About the Technical ReVieWer ...-:esscseseseseeies
Trang 1
Develop cloud-ready web applications
using Microsofts latest framework,
ASP.NET Core MVC 2
Seventh Edition
Trang 2Pro ASP.NET Core MVC 2
Seventh Edition
Adam Freeman
Apress’
Trang 3Pro ASP.NET Core MVC2
Adam Freeman
London, UK
ISBN-13 (pbk): 978-1-4842-3149-4 ISBN-13 (electronic): 978-1-4842-3150-0 https://doi.org/10.1007/978-1-4842-3150-0
Library of Congress Control Number: 2017958033
Copyright © 2017 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
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
Cover image by Freepik (www freepik con)
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Gwenan Spearing
Development Editor: Laura Berendson
Technical Reviewer: Fabio Claudio Ferracchiati
Coordinating Editor: Mark Powers
Copy Editor: Kim Wimpsett
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
orders-ny@springer-sbm.com, 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 rights@apress com, or visit www apress.com/
rights-permissions
Apress titles 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 Print and eBook Bulk Sales web page at www apress.com/bulk-sales
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress com/9781484231494 For more
detailed information, please visit www.apress com/source-code
Printed on acid-free paper
Trang 4Dedicated to my lovely wife, Jacqui Griffyth
(And also to Peanut.)
Trang 5Contents at a Glance
About the Technical ReVieWer .-:esscseseseseeieseirresrsrriisrinsnsnnise XXiX Part I: Introducing ASP.NET Core MVC 2 . 5-«<5< 5< L Chapter 1: ASP NET Core MVC in Cornfex† - c5 3 Chapter 2: Your First MVG Applicafioin .- c5 c5 Siseeeseesee 11 WChapter 3: The MVC Pattern, Projects, and Conventions . . - 53 Chapter 4: Essential C# Fea†ur@s Hy 67 WChapter 5: Working with RZ0r . «5c S« Series 105 Chapter 6: Working with Visual S†udi0 «- «<5 5<5<5<<5<cs<ssesssses 127 Chapter 7: Unit Testing MVG Applicafionns .- -‹-c<5 +55 163 WChapter 8: SportsStore: A Real Applicafion «-c<c<c<seeieee 193 WChapter 9: SportsStore: Navigation .sssssssssssssssecsesssecseeseeesseseeenseeee 237 Chapter 10: SportsStore: Completing the Œarf «<< 271 Chapter 11: SportsStore: Administrafion .«cÄĂ565Seiee 295 WChapter 12: SportsStore: Security and Deploymmen† -‹‹-‹-< 323 WChapter 13: Working with Visual Studio Code ‹‹-««<5<5<<5<<+< 349 Part II: ASP.NET Gore MVC 2 in Detail - 373 WChapter 14: Configuring Applica†i0nns c« c5 Series 375 Chapter 15: URL Roufig -. 5c Sen 429 WChapter 16: Advanced Routing Fea†ures .-. «<< 469
Trang 6CONTENTS AT A GLANCE
Chapter 17: Controllers and Actions 507 Chapter 18: Dependency Injection ccsccsescseeeecseeseeessesseeecseeseeecseseeeee 551 Chapter 19: Filt@rS ch HH ghê 585 Chapter 20: API Œonnfr0ll©rS . cccccSetkikireererrrrie 625 Chapter 21: WieWS «HH HH ghê 657 Chapter 22: View 0Imp0TI€TIfS .- 5c « Site 691 Chapter 23: Understanding Tag HelperS -.- . -« c<cs<cseseeese 721 Chapter 24: Using the Form Tag Helpers . -‹-«5<c5<c<csscseese 755 MChapter 25: Using the Other Built-in Tag Helpers 781 Chapter 26: Model Bindinng «c5 si 811 Chapter 27: Model Validafior . « «5< c «Series 849 Chapter 28: Getting Started with IdenifW «-«5c<csesesee 881 MChapter 29: Applying ASP.NET Core Idlenifify -«5<cc<c<essseeese 919 Chapter 30: Advanced ASP.NET Core Idlenfify .e c<cc<csesseeesse 949 Chapter 31: Model Conventions and Action Gonstrainis .- 983 Index
vi
Trang 7Contents
About te Auf0r .eeccscsssesesksEsrsitrritrrsrsrriiiiinrarirasisnaasnanaraasi XXVii About the Technical ReVieWer .- -:esseseseseseesesesrsresrsrsrrirriansnarsrsiee XXiX Part I: Introducing ASP.NET Core MVC 2 . -«<-«<<<<+< 1 Chapter 1: ASP NET Gore MVC ïn Gonif©eX « «5S 3 Understanding the History of ASP.NET Core IMV ¿- 5c +ccs+ecvsvexexexereeree 3 ASP.NET Web Forms
The Original MVC Framework
Understanding ASP.NET COf6 - ¿+ + tt EkEEkEEkEkrkkEkkkekkkrkkkekrkkekrrkrree 5 What's New in ASP.NET Core MVC 2
Key Benefits of ASPNET Core MVC What Do | Need to KNOW? cesseesssessessesssesssesssesssesssesssesssesssesssesssessstssstesseesntesneesntesneenee 9 What Is the Structure Of This BOOK? csssesssesssessessessessessesseesessesseesessseeseeseesneenee 9 Part 1: Introducing ASP.NET Core MVC
Part 2: ASP.NET Core MVC in Detail Where Can You Get the Example Code? .cccssessssesssesessessssesessesessesesseesseetsseeseeeseeeees 9 Where Can You Get Corrections for This BOOK? .ssssssessssesseesseesseesseesseesseesseesseesseesneess 10
10
10
Summary
Chapter 2: Your First MVC Application 11
Installing the NET Core 2.0 SIK - ¿- + St$t‡E$keEvEkeEkkkekrkkerrkkerrrkerrkerek 12
vii
Trang 8CONTENTS:
Creating a New ASP.NET Core MVC Project
Rendering Web Pages
Creating a Simple Data-Entry Application
Summary
Chapter 3: The MVC Pattern, Projects, and Conventions
The History of MVC
Understanding the MVC Pattern
Comparing MVC to Other Patterns
viii
Adding the Controller
Understanding Routes
Creating and Rendering a View
Adding Dynamic Output
Setting the Scene
Designing a Data Model
Creating a Second Action and a Strongly Typed View
Linking Action Methods
Building the Form
Receiving Form Data
Displaying the Responses
Adding Validation
Styling the Content
Understanding Models
Understanding Controllers
Understanding Views
The ASP.NET Implementation of MVC
Understanding the Smart UI Pattern
Understanding the Model-View Architecture
Understanding Classic Three-Tier Architecture
Understanding Variations on MVC
Trang 9CONTENTS
Understanding ASP.NET Core MVC Projects
Creating the Project
Understanding MVC Conventions
Chapter 4: Essential G# Feafur@s ĂĂĂSSeearsssses ÔỬ Preparing the Exaimple Pf0j@GÌ ¿ - cS+tS‡kEkEkeEkEkeEkkkekrkkrrkkerrrkerrkerkek 68 Enabling ASP.NET Core MVC
Creating the MVC Application Components
Using the Null Conditional Operator .ccescssessssesessestssestsseetsseetsseetsseetsseensseeteseeesseeees 73 Chaining the Null Conditional Operator
Combining the Conditional and Coalescing Operators
Using Automatically Implemented Proper†i@S -‹ - 5c ++secvsxecvsxexesxerex
Using Auto-Implemented Property Initializers
Creating Read-Only Automatically Implemented Properties
Using String Interpolation
Using Object and Collection Initializers
Using an Index Initializer
Pattern Matching
Pattern Matching in Switch Statements
Using Extension Methods .c.ccccscccsecsssessssessssesessesesseessseensseessessseeesseeseeesseeesseeeseeeees
Applying Extension Methods to an Interface
Creating Filtering Extension Methods
Using Lambda Expressions .ccscccssessssessssecsssesessesesseessessseesseesseesseeseeesseeseeesseeees 90
Defining Functions
Using Lambda Expression Methods and Properties
Using Type Inference and Anonymous Type:
Using Anonymous Types
Using Asynchronous IMl@ffI0(S ‹ - ¿+ Sx+x‡Ev£eEvEkeEvkkeEkkerrkerrkerrkerxek 99 Working with Tasks Directly
Applying the async and await Keywords
Trang 10
CONTENTS
MChapter 5: Working with Razor sees 105 Preparing the Example Project
Defining the Model
Creating the Controller
Creating the View
Working with the Model Object
Using View Imports
Working with Layouts
Creating the Layout
Applying a Layout
Using a View Start File
Using Razor Expressions
Inserting Data Values
Setting Attribute Values
Using Conditional Statements
Enumerating Arrays and Collections
Summary
Chapter 6: Working with Visual Studio
Preparing the Example Project
Creating the Model
Creating the Controller and View
Managing Software Packages 131
Understanding NuGe‡
Understanding Bower
Understanding Iterative Development
Making Changes to Razor Views
Making Changes to C# Classes
Using Browser Link.