M A N N I N G Andrew Lock Useful .NET CLI (dotnet) commands. Use --help to see all the optional arguments Command Description Run from dotnet restore Restores the NuGet packages for all the Solution folder projects in the solution. dotnet build Builds all the projects in the solution. To Solution folder build in Release mode, use the -c switch. dotnet run Runs the project in the current folder. Use Project folder during development to run your app. dotnet publish -c Release –o Publishes the project to the provided Project folder <Folder> folder. This copies all the required files to the provided output folder so that it can be deployed. dotnet test Builds the project and executes any unit Project folder tests found in the project. Requires the .NET Test SDK and a testing framework adapter (see chapter 20). dotnet add package <Name> Installs the NuGet package with the pro- Project folder vided name into the current project. Optionally, specify a package version, for example -v 2.1.0. dotnet new --list Views all the installed templates for creat- Anywhere ing ASP.NET Core apps, libraries, test proj- ects, solution files, and much more. dotnet new --install Installs a new template from the provided Anywhere <PackageName> NuGet package name. For example, dotnet new --install "NetEscapades.Templates::*". dotnet new <Template> –o <Folder> Creates a new item from the provided tem- Anywhere, empty -n <NewName> plate, specifying the folder in which to folder for new place the item, and the name for the item. projects dotnet ef --help Displays help for managing your database Project folder and migrations using EF Core. Requires the EF Core tools (see chapter 12). dotnet ef migrations add <Name> Adds a new EF Core migration to the proj- Project folder ect with the provided name. dotnet ef database update Applies pending EF Core migrations to a Project folder database. Warning—this will modify your database! ASP.NET Core in Action ASP.NET Core in Action ANDREW LOCK MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2018 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Marina Michaels 20 Baldwin Road Technical development editor: Kris Athi PO Box 761 Review editor: Ivan Martinovic´ Shelter Island, NY 11964 Project manager: David Novak Copyeditor: Safis Editing Proofreader: Elizabeth Martin Technical proofreader: Tanya Wilke Typesetter: Dottie Marsico Cover designer: Marija Tudor ISBN 9781617294617 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 23 22 21 20 19 18 brief contents PART 1 GETTING STARTED WITH MVC..........................................1 1 ■ Getting started with ASP.NET Core 3 2 ■ Your first application 28 3 ■ Handling requests with the middleware pipeline 61 4 ■ Creating web pages with MVC controllers 93 5 ■ Mapping URLs to methods using conventional routing 120 6 ■ The binding model: retrieving and validating user input 148 7 ■ Rendering HTML using Razor views 174 8 ■ Building forms with Tag Helpers 204 9 ■ Creating a Web API for mobile and client applications using MVC 234 PART 2 BUILDING COMPLETE APPLICATIONS..............................265 10 ■ Service configuration with dependency injection 267 11 ■ Configuring an ASP.NET Core application 303 12 ■ Saving data with Entity Framework Core 334 13 ■ The MVC filter pipeline 369 14 ■ Authentication: adding users to your application with Identity 400 15 ■ Authorization: securing your application 432 16 ■ Publishing and deploying your application 461 v vi BRIEF CONTENTS PART 3 EXTENDING YOUR APPLICATIONS...................................499 17 ■ Monitoring and troubleshooting errors with logging 501 18 ■ Improving your application’s security 534 19 ■ Building custom components 572 20 ■ Testing your application 607 contents preface xvii acknowledgments xix about this book xxi about the author xxv about the cover illustration xxvi PART 1 GETTING STARTED WITH MVC...........................1 1 Getting started with ASP.NET Core 3 1.1 An introduction to ASP.NET Core 4 Using a web framework 4 ■ The benefits and limitations of ASP.NET 5 ■ What is ASP.NET Core? 6 1.2 When to choose ASP.NET Core 8 What type of applications can you build? 8 ■ If you’re new to .NET development 10 ■ If you’re a .NET Framework developer creating a new application 12 ■ Converting an existing ASP.NET application to ASP.NET Core 16 1.3 How does ASP.NET Core work? 17 How does an HTTP web request work? 17 ■ How does ASP.NET Core process a request? 19 1.4 Choosing a platform for ASP.NET Core 21 Advantages of using .NET Framework 21 ■ Advantages of using .NET Core 22 vii viii CONTENTS 1.5 Preparing your development environment 23 If you’re a Windows user 24 ■ If you’re a Linux or macOS user 25 2 Your first application 28 2.1 A brief overview of an ASP.NET Core application 29 2.2 Creating your first ASP.NET Core application 32 Using a template to get started 32 ■ Building the application 35 2.3 Running the web application 36 2.4 Understanding the project layout 38 2.5 The csproj project file: defining your dependencies 40 2.6 The Program class: building a web host 41 2.7 The Startup class: configuring your application 44 Adding and configuring services 45 ■ Defining how requests are handled with middleware 47 2.8 MVC middleware and the home controller 52 2.9 Generating HTML with Razor template views 54 3 Handling requests with the middleware pipeline 61 3.1 What is middleware? 63 3.2 Combining middleware in a pipeline 66 Simple pipeline scenario 1: a holding page 67 ■ Simple pipeline scenario 2: Handling static files 70 ■ Simple pipeline scenario 3: An MVC web application 73 3.3 Handling errors using middleware 78 Viewing exceptions in development: DeveloperExceptionPage 80 ■ Handling exceptions in production: ExceptionHandlerMiddleware 82 Handling other errors: StatusCodePagesMiddleware 86 Disabling error handling middleware for Web APIs 90 4 Creating web pages with MVC controllers 93 4.1 An introduction to MVC 95 The MVC design pattern 95 ■ MVC in ASP.NET Core 98 Adding the MvcMiddleware to your application 104 ■ What makes a controller a controller? 111 4.2 MVC controllers and action methods 113 Accepting parameters to action methods 114 ■ Using ActionResult 116
Description: