E-Book, Englisch, 1018 Seiten
FREEMAN Pro ASP.NET Core MVC
6th ed
ISBN: 978-1-4842-0397-2
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
E-Book, Englisch, 1018 Seiten
ISBN: 978-1-4842-0397-2
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
Visual Studio 2017 updates for this book are now available. Follow the Download Source Code link for this book on the Apress website.
Now in its 6th edition, the best selling book on MVC is now updated for ASP.NET Core MVC. It contains detailed explanations of the new Core MVC functionality which enables developers to produce leaner, cloud optimized and mobile-ready applications for the .NET platform. This book puts ASP.NET Core MVC into context and dives deep into the tools and techniques required to build modern, cloud optimized extensible web applications. All the new MVC features are described in detail and the author explains how best to apply them to both new and existing projects.
The ASP.NET Core MVC Framework is the latest evolution of Microsoft’s ASP.NET web platform, built on a completely new foundation. It represents a fundamental change to how Microsoft constructs and deploys web frameworks and is free of the legacy of earlier technologies such asWeb Forms. ASP.NET Core MVC provides a "host agnostic" framework and a high-productivity programming model that promotes cleaner code architecture, test-driven development, and powerful extensibility.Best-selling author Adam Freeman has thoroughly revised this market-leading book and explains how to get the most from ASP.NET Core MVC. He starts with the nuts-and-bolts and shows you everything through to advanced features, going in-depth to give you the knowledge you need.
This book follows the same format and style as the popular previous editions but brings everything up to date for the new ASP.NET Core MVC release. It presents a fully worked case study of a functioning ASP.NET MVC application that readers can use as a template for their own projects.What You Will Learn:
- Gain a solid architectural understanding of ASP.NET Core MVC
- Explore the entire ASP.NET MVC Framework as a cohesive whole
- Seehow MVC and test-driven development work in action
- Learn what's new in ASP.NET Core MVC and how best to apply these new features to your own work
- See how to create RESTful web services and Single Page Applications
- Build on your existing knowledge of previous MVC releases to get up and running with the new programming model quickly and effectively
Who This Book Is For:
This book is for web developers with a basic knowledge of ASP.NET and C# who want to incorporate the latest improvements and functionality in the new ASP.NET Core MVC Framework.
Autoren/Hrsg.
Weitere Infos & Material
1;Contents at a Glance;5
2;Contents;7
3;About the Author;27
4;About the Technical Reviewer;28
5;Part I: Introducing ASP.NET Core MVC;29
5.1;Chapter 1: ASP.NET Core MVC in Context;30
5.1.1;Understanding the History of ASP.NET Core MVC;30
5.1.1.1;ASP.NET Web Forms;30
5.1.1.1.1;What Was Wrong with ASP.NET Web Forms?;31
5.1.1.2;The Original MVC Framework;32
5.1.1.2.1;What Was Wrong with the Original MVC Framework?;32
5.1.2;Understanding ASP.NET Core;32
5.1.2.1;Key Benefits of ASP.NET Core MVC;33
5.1.2.1.1;MVC Architecture;33
5.1.2.1.2;Extensibility;33
5.1.2.1.3;Tight Control over HTML and HTTP;33
5.1.2.1.4;Testability;34
5.1.2.1.5;Powerful Routing System;34
5.1.2.1.6;Modern API;35
5.1.2.1.7;Cross-Platform;35
5.1.2.1.8;ASP.NET Core MVC Is Open Source;35
5.1.3;What Do I Need to Know?;35
5.1.4;What Is the Structure of This Book?;35
5.1.4.1;Part 1: Introducing ASP.NET Core MVC;35
5.1.4.2;Part 2: ASP.NET Core MVC in Detail;36
5.1.5;What’s New in This Edition?;36
5.1.6;Where Can I Get the Example Code?;36
5.1.7;Summary;36
5.2;Chapter 2: Your First MVC Application;37
5.2.1;Installing Visual Studio;37
5.2.2;Creating a New ASP.NET Core MVC Project;39
5.2.2.1;Adding the Controller;43
5.2.2.2;Understanding Routes;45
5.2.3;Rendering Web Pages;46
5.2.3.1;Creating and Rendering a View;46
5.2.3.2;Adding Dynamic Output;49
5.2.4;Creating a Simple Data-Entry Application;51
5.2.4.1;Setting the Scene;51
5.2.4.2;Designing a Data Model;52
5.2.4.3;Creating a Second Action and a Strongly Typed View;53
5.2.4.4;Linking Action Methods;54
5.2.4.5;Building the Form;56
5.2.4.6;Receiving Form Data;57
5.2.4.6.1;Using Model Binding;59
5.2.4.6.2;Storing Responses;60
5.2.4.7;Displaying the Responses;62
5.2.4.8;Adding Validation;64
5.2.4.8.1;Highlighting Invalid Fields;68
5.2.4.9;Styling the Content;71
5.2.4.9.1;Styling the Welcome View;71
5.2.4.9.2;Styling the RsvpForm View;73
5.2.4.9.3;Styling the Thanks View;75
5.2.4.9.4;Styling the List View;76
5.2.5;Summary;77
5.3;Chapter 3: The MVC Pattern, Projects, and Conventions;78
5.3.1;The History of MVC;78
5.3.2;Understanding the MVC Pattern;78
5.3.2.1;Understanding Models;79
5.3.2.2;Understanding Controllers;79
5.3.2.3;Understanding Views;80
5.3.2.4;The ASP.NET Implementation of MVC;80
5.3.3;Comparing MVC to Other Patterns;80
5.3.3.1;Understanding the Smart UI Pattern;81
5.3.3.2;Understanding the Model-View Architecture;82
5.3.3.3;Understanding Classic Three-Tier Architectures;82
5.3.3.4;Understanding Variations on MVC;83
5.3.3.4.1;Understanding the Model-View-Presenter Pattern;83
5.3.3.4.2;Understanding the Model-View-View Model Pattern;83
5.3.4;Understanding ASP.NET Core MVC Projects;84
5.3.4.1;Creating the Project;84
5.3.4.2;Understanding MVC Conventions;87
5.3.4.2.1;Following Conventions for Controller Classes;87
5.3.4.2.2;Following Conventions for Views;88
5.3.4.2.3;Following Conventions for Layouts;88
5.3.5;Summary;89
5.4;Chapter 4: Essential C# Features;90
5.4.1;Preparing the Example Project;90
5.4.1.1;Enabling ASP.NET Core MVC;92
5.4.1.2;Creating the MVC Application Components;93
5.4.1.2.1;Creating the Model;93
5.4.1.2.2;Creating the Controller and View;94
5.4.2;Using the Null Conditional Operator;95
5.4.2.1;Chaining the Null Conditional Operator;96
5.4.2.2;Combining the Conditional and Coalescing Operators;97
5.4.3;Using Automatically Implemented Properties;98
5.4.3.1;Using Auto-Implemented Property Initializers;99
5.4.3.2;Creating Read-Only Automatically Implemented Properties;100
5.4.4;Using String Interpolation;101
5.4.5;Using Object and Collection Initializers;102
5.4.5.1;Using an Index Initializer;104
5.4.6;Using Extension Methods;105
5.4.6.1;Applying Extension Methods to an Interface;107
5.4.6.2;Creating Filtering Extension Methods;108
5.4.7;Using Lambda Expressions;110
5.4.7.1;Defining Functions;111
5.4.7.2;Using Lambda Expression Methods and Properties;114
5.4.8;Using Type Inference and Anonymous Types;116
5.4.8.1;Using Anonymous Types;117
5.4.9;Using Asynchronous Methods;119
5.4.9.1;Working with Tasks Directly;119
5.4.9.2;Applying the async and await Keywords;121
5.4.10;Getting Names;122
5.4.11;Summary;124
5.5;Chapter 5: Working with Razor;125
5.5.1;Preparing the Example Project;126
5.5.1.1;Defining the Model;127
5.5.1.2;Creating the Controller;127
5.5.1.3;Creating the View;128
5.5.2;Working with the Model Object;129
5.5.2.1;Using View Imports;131
5.5.3;Working with Layouts;133
5.5.3.1;Creating the Layout;133
5.5.3.2;Applying a Layout;135
5.5.3.3;Using a View Start File;136
5.5.4;Using Razor Expressions;138
5.5.4.1;Inserting Data Values;139
5.5.4.2;Setting Attribute Values;141
5.5.4.3;Using Conditional Statements;142
5.5.4.4;Enumerating Arrays and Collections;144
5.5.5;Summary;146
5.6;Chapter 6: Working with Visual Studio;147
5.6.1;Preparing the Example Project;147
5.6.1.1;Creating the Model;148
5.6.1.2;Creating the Controller and View;150
5.6.2;Managing Software Packages;152
5.6.2.1;Understanding NuGet;152
5.6.2.1.1;Understanding the NuGet Packages List and Location;153
5.6.2.2;Understanding Bower;154
5.6.2.2.1;Understanding the Bower Packages List;154
5.6.3;Understanding Iterative Development;158
5.6.3.1;Making Changes to Razor Views;158
5.6.3.2;Making Changes to C# Classes;160
5.6.3.2.1;Compiling Classes Automatically;160
5.6.3.2.2;Enabling Developer Exception Pages;162
5.6.3.2.3;Using the Debugger;163
5.6.3.2.3.1;Setting a Breakpoint;164
5.6.3.2.3.2;Viewing Data Values in the Code Editor;165
5.6.3.2.3.3;Using the Locals Window;167
5.6.3.3;Using Browser Link;168
5.6.3.3.1;Setting Up Browser Link;168
5.6.3.3.2;Using Browser Link;169
5.6.3.3.3;Using Multiple Browsers;171
5.6.4;Preparing JavaScript and CSS for Deployment;174
5.6.4.1;Enabling Static Content Delivery;174
5.6.4.2;Adding Static Content to the Project;175
5.6.4.3;Updating the View;177
5.6.4.4;Bundling and Minifying in MVC Applications;178
5.6.4.4.1;Installing the Visual Studio Extension;178
5.6.4.4.2;Bundling and Minifying Files;179
5.6.5;Summary;182
5.7;Chapter 7: Unit Testing MVC Applications;183
5.7.1;Preparing the Example Project;184
5.7.1.1;Enabling the Built-in Tag Helpers;184
5.7.1.2;Adding Actions to the Controller;184
5.7.1.3;Creating the Data Entry Form;185
5.7.1.4;Updating the Index View;186
5.7.2;Unit Testing MVC Applications;187
5.7.2.1;Creating a Unit test Project;188
5.7.2.1.1;Configuring the Unit Test Project;189
5.7.2.1.2;Adding the Application Project Reference;190
5.7.2.2;Writing and Running Unit Tests;191
5.7.2.2.1;Running Tests with the Test Explorer;193
5.7.2.3;Isolating Components for Unit Testing;195
5.7.2.3.1;Isolating a Component;197
5.7.3;Improving Unit Tests;203
5.7.3.1;Parameterizing a Unit Test;203
5.7.3.1.1;Getting Test Data from a Method or Property;205
5.7.3.2;Improving Fake Implementations;207
5.7.3.2.1;Adding a Mocking Framework;209
5.7.3.2.2;Creating a Mock Object;211
5.7.4;Summary;213
5.8;Chapter 8: SportsStore: A Real Application;214
5.8.1;Getting Started;215
5.8.1.1;Creating the MVC Project;215
5.8.1.1.1;Adding the NuGet Packages;216
5.8.1.1.2;Creating the Folder Structure;218
5.8.1.1.3;Configuring the Application;218
5.8.1.2;Creating the Unit Test Project;220
5.8.1.3;Checking and Running the Application;222
5.8.2;Starting the Domain Model;223
5.8.2.1;Creating a Repository;223
5.8.2.2;Creating a Fake Repository;224
5.8.2.3;Registering the Repository Service;224
5.8.3;Displaying a List of Products;225
5.8.3.1;Adding a Controller;227
5.8.3.2;Adding and Configuring the View;228
5.8.3.3;Setting the Default Route;230
5.8.3.4;Running the Application;231
5.8.4;Preparing a Database;231
5.8.4.1;Installing Entity Framework Core;232
5.8.4.2;Creating the Database Classes;233
5.8.4.3;Creating the Repository Class;235
5.8.4.4;Defining the Connection String;235
5.8.4.5;Configuring the Application;236
5.8.4.6;Creating and Applying the Database Migration;238
5.8.5;Adding Pagination;239
5.8.5.1;Displaying Page Links;241
5.8.5.1.1;Adding the View Model;242
5.8.5.1.2;Adding the Tag Helper Class;242
5.8.5.1.3;Adding the View Model Data;245
5.8.5.1.4;Displaying the Page Links;248
5.8.5.2;Improving the URLs;250
5.8.6;Styling the Content;251
5.8.6.1;Installing the Bootstrap Package;252
5.8.6.2;Applying Bootstrap Styles to the Layout;252
5.8.6.3;Creating a Partial View;255
5.8.7;Summary;257
5.9;Chapter 9: SportsStore: Navigation;258
5.9.1;Adding Navigation Controls;258
5.9.1.1;Filtering the Product List;258
5.9.1.2;Refining the URL Scheme;262
5.9.1.3;Building a Category Navigation Menu;266
5.9.1.3.1;Creating the Navigation View Component;266
5.9.1.3.2;Generating Category Lists;268
5.9.1.3.3;Creating the View;269
5.9.1.3.4;Highlighting the Current Category;271
5.9.1.4;Correcting the Page Count;274
5.9.2;Building the Shopping Cart;276
5.9.2.1;Defining the Cart Model;277
5.9.2.2;Adding the Add to Cart Buttons;281
5.9.2.3;Enabling Sessions;283
5.9.2.4;Implementing the Cart Controller;284
5.9.2.4.1;Defining Session State Extension Methods;286
5.9.2.5;Displaying the Contents of the Cart;287
5.9.3;Summary;290
5.10;Chapter 10: SportsStore: Completing the Cart;291
5.10.1;Refining the Cart Model with a Service;291
5.10.1.1;Creating a Storage-Aware Cart Class;291
5.10.1.2;Registering the Service;292
5.10.1.3;Simplifying the Cart Controller;293
5.10.2;Completing the Cart Functionality;294
5.10.2.1;Removing Items from the Cart;294
5.10.2.2;Adding the Cart Summary Widget;296
5.10.2.2.1;Adding the Font Awesome Package;296
5.10.2.2.2;Creating the View Component Class and View;297
5.10.3;Submitting Orders;299
5.10.3.1;Creating the Model Class;299
5.10.3.2;Adding the Checkout Process;300
5.10.3.3;Implementing Order Processing;304
5.10.3.3.1;Extending the Database;304
5.10.3.3.2;Creating the Order Repository;305
5.10.3.4;Completing the Order Controller;307
5.10.3.5;Displaying Validation Errors;310
5.10.3.6;Displaying a Summary Page;312
5.10.4;Summary;312
5.11;Chapter 11: SportsStore: Administration;313
5.11.1;Managing Orders;313
5.11.1.1;Enhancing the Model;313
5.11.1.2;Adding the Actions and View;314
5.11.2;Adding Catalog Management;317
5.11.2.1;Creating a CRUD Controller;318
5.11.2.2;Implementing the List View;320
5.11.2.3;Editing Products;321
5.11.2.3.1;Creating the Edit Action Method;322
5.11.2.3.2;Creating the Edit View;323
5.11.2.3.3;Updating the Product Repository;325
5.11.2.3.4;Handling Edit POST Requests;326
5.11.2.3.5;Displaying a Confirmation Message;329
5.11.2.3.6;Adding Model Validation;330
5.11.2.3.7;Enabling Client-Side Validation;333
5.11.2.4;Creating New Products;335
5.11.2.5;Deleting Products;337
5.11.3;Summary;340
5.12;Chapter 12: SportsStore: Security and Deployment;341
5.12.1;Securing the Administration Features;341
5.12.1.1;Adding the Identity Package to the Project;341
5.12.1.2;Creating the Identity Database;342
5.12.1.2.1;Creating the Context Class;342
5.12.1.2.2;Defining the Connection String;343
5.12.1.2.3;Configuring the Application;343
5.12.1.2.4;Defining the Seed Data;345
5.12.1.2.5;Creating and Applying the Database Migration;345
5.12.1.3;Applying a Basic Authorization Policy;346
5.12.1.4;Creating the Account Controller and Views;348
5.12.1.5;Testing the Security Policy;352
5.12.2;Deploying the Application;352
5.12.2.1;Creating the Databases;353
5.12.2.1.1;Opening Firewall Access for Configuration;353
5.12.2.1.2;Getting the Connection Strings;354
5.12.2.2;Preparing the Application;354
5.12.2.2.1;Creating the Error Controller and View;354
5.12.2.2.2;Defining the Production Database Settings;355
5.12.2.2.3;Configuring the Application;356
5.12.2.2.4;Updating the Project Configuration;358
5.12.2.3;Applying the Database Migrations;359
5.12.2.4;Deploying the Application;359
5.12.3;Summary;364
5.13;Chapter 13: Working with Visual Studio Code;365
5.13.1;Setting Up the Development Environment;365
5.13.1.1;Installing Node.js;365
5.13.1.1.1;Installing Node.js on Windows;366
5.13.1.1.2;Installing Node.js on OS X/macOS;366
5.13.1.1.3;Installing Node.js on Linux;367
5.13.1.2;Checking the Node Installation;367
5.13.1.3;Installing Git;367
5.13.1.3.1;Installing Git on Windows or OS X/macOS;367
5.13.1.3.2;Installing Git on Linux;367
5.13.1.4;Checking the Git Installation;367
5.13.1.5;Installing Yeoman, Bower, and Gulp;368
5.13.1.6;Installing .NET Core;368
5.13.1.6.1;Installing .NET Core on Windows;368
5.13.1.6.2;Installing .NET Core on OS X/macOS;369
5.13.1.6.3;Installing .NET Core on Linux;369
5.13.1.7;Checking the .NET Core Installation;369
5.13.1.8;Installing Visual Studio Code;370
5.13.1.8.1;Installing Visual Studio Code on Windows;370
5.13.1.8.2;Installing Visual Studio Code on OS X/macOS;370
5.13.1.8.3;Installing Visual Studio Code on Linux;370
5.13.1.9;Checking the Visual Studio Code Installation;370
5.13.1.10;Installing the Visual Studio Code C# Extension;371
5.13.2;Creating an ASP.NET Core Project;372
5.13.3;Preparing the Project with Visual Studio Code;373
5.13.3.1;Adding NuGet Packages to the Project;374
5.13.3.2;Adding Client-Side Packages to the Project;375
5.13.3.3;Configuring the Application;377
5.13.3.4;Building and Running the Project;377
5.13.4;Re-creating the PartyInvites Application;378
5.13.4.1;Creating the Model and Repository;378
5.13.4.2;Creating the Database;381
5.13.4.2.1;Adding the Database Packages;382
5.13.4.2.2;Creating and Applying the Database Migration;383
5.13.4.3;Creating the Controllers and Views;383
5.13.5;Unit Testing in Visual Studio Code;388
5.13.5.1;Configuring the Application;388
5.13.5.2;Creating a Unit Test;389
5.13.5.3;Running Tests;390
5.13.6;Summary;391
6;Part II: ASP.NET Core MVC in Detail;392
6.1;Chapter 14: Configuring Applications;393
6.1.1;Preparing the Example Project;394
6.1.2;Understanding the JSON Configuration Files;396
6.1.2.1;Configuring the Solution;397
6.1.2.2;Configuring the Project;399
6.1.2.2.1;Adding Dependencies to the project.json File;400
6.1.2.2.2;Registering Development Tools in the project.json File;401
6.1.3;Understanding the Program Class;402
6.1.4;Understanding the Startup Class;403
6.1.4.1;Understanding How the Startup Class Is Used;405
6.1.4.2;Understanding ASP.NET Services;406
6.1.4.2.1;Understanding the MVC Services;408
6.1.4.3;Understanding ASP.NET Middleware;409
6.1.4.3.1;Creating Content-Generating Middleware;409
6.1.4.3.1.1;Using Services in Middleware;411
6.1.4.3.2;Creating Short-Circuiting Middleware;412
6.1.4.3.3;Creating Request-Editing Middleware;414
6.1.4.3.4;Creating Response-Editing Middleware;416
6.1.4.4;Understanding How the Configure Method Is Invoked;418
6.1.4.4.1;Using the Application Builder;419
6.1.4.4.2;Using the Hosting Environment;420
6.1.4.4.3;Using the Logging Factory;422
6.1.4.4.3.1;Creating Custom Log Entries;425
6.1.4.5;Adding the Remaining Middleware Components;427
6.1.4.5.1;Enabling Exception Handling;427
6.1.4.5.2;Enabling Browser Link;429
6.1.4.5.3;Enabling Static Content;430
6.1.4.6;Using Configuration Data;432
6.1.4.6.1;Reading Configuration Data;432
6.1.4.6.2;Using Configuration Data;435
6.1.4.6.3;Using Configuration Data for Built-in Middleware Components;437
6.1.5;Configuring MVC Services;438
6.1.6;Dealing with Complex Configurations;440
6.1.6.1;Creating Different External Configuration Files;440
6.1.6.2;Creating Different Configuration Methods;441
6.1.6.3;Creating Different Configuration Classes;442
6.1.7;Summary;444
6.2;Chapter 15: URL Routing;445
6.2.1;Preparing the Example Project;447
6.2.1.1;Creating the Model Class;448
6.2.1.2;Creating the Example Controllers;449
6.2.1.3;Creating the View;450
6.2.2;Introducing URL Patterns;451
6.2.3;Creating and Registering a Simple Route;453
6.2.4;Defining Default Values;454
6.2.4.1;Defining Inline Default Values;455
6.2.5;Using Static URL Segments;457
6.2.6;Defining Custom Segment Variables;462
6.2.6.1;Using Custom Variables as Action Method Parameters;464
6.2.6.2;Defining Optional URL Segments;466
6.2.6.3;Defining Variable-Length Routes;468
6.2.7;Constraining Routes;471
6.2.7.1;Constraining a Route Using a Regular Expression;474
6.2.7.2;Using Type and Value Constraints;475
6.2.7.3;Combining Constraints;476
6.2.7.4;Defining a Custom Constraint;477
6.2.7.4.1;Defining an Inline Custom Constraint;479
6.2.8;Using Attribute Routing;480
6.2.8.1;Preparing for Attribute Routing;480
6.2.8.2;Applying Attribute Routing;481
6.2.8.2.1;Changing the Name of an Action Method;482
6.2.8.2.2;Creating a More Complex Route;483
6.2.8.3;Applying Route Constraints;484
6.2.9;Summary;484
6.3;Chapter 16: Advanced Routing Features;485
6.3.1;Preparing the Example Project;486
6.3.2;Generating Outgoing URLs in Views;488
6.3.2.1;Generating Outgoing Links;488
6.3.2.1.1;Targeting Other Controllers;492
6.3.2.1.2;Passing Extra Values;493
6.3.2.1.3;Generating Fully Qualified URLs;496
6.3.2.1.4;Generating a URL from a Specific Route;497
6.3.2.2;Generating URLs (and Not Links);499
6.3.2.2.1;Generating URLs in Action Methods;499
6.3.3;Customizing the Routing System;500
6.3.3.1;Changing the Routing System Configuration;501
6.3.3.2;Creating a Custom Route Class;502
6.3.3.2.1;Routing Incoming URLs;503
6.3.3.2.1.1;Applying a Custom Route Class;505
6.3.3.2.1.2;Routing to MVC Controllers;507
6.3.3.2.2;Generating Outgoing URLs;510
6.3.4;Working with Areas;513
6.3.4.1;Creating an Area;513
6.3.4.2;Creating an Area Route;514
6.3.4.3;Populating an Area;515
6.3.4.4;Generating Links to Actions in Areas;517
6.3.5;URL Schema Best Practices;519
6.3.5.1;Make Your URLs Clean and Human-Friendly;519
6.3.5.2;GET and POST: Pick the Right One;520
6.3.6;Summary;521
6.4;Chapter 17: Controllers and Actions;522
6.4.1;Preparing the Example Project;523
6.4.1.1;Preparing the Views;525
6.4.2;Understanding Controllers;527
6.4.3;Creating Controllers;527
6.4.3.1;Creating POCO Controllers;527
6.4.3.1.1;Using the MVC Controller API;529
6.4.3.2;Using the Controller Base Class;530
6.4.4;Receiving Context Data;531
6.4.4.1;Getting Data from Context Objects;531
6.4.4.1.1;Getting Context Data in a POCO Controller;533
6.4.4.2;Using Action Method Parameters;536
6.4.5;Producing a Response;538
6.4.5.1;Producing a Response Using the Context Object;538
6.4.5.2;Understanding Action Results;539
6.4.5.3;Producing an HTML Response;541
6.4.5.3.1;Understanding the Search for a View File;542
6.4.5.3.2;Passing Data from an Action Method to a View;545
6.4.5.3.2.1;Using a View Model Object;545
6.4.5.3.3;Passing Data with the View Bag;548
6.4.5.4;Performing Redirections;550
6.4.5.4.1;Redirecting to a Literal URL;550
6.4.5.4.2;Redirecting to a Routing System URL;552
6.4.5.4.3;Redirecting to an Action Method;553
6.4.5.4.4;Using the Post/Redirect/Get Pattern;555
6.4.5.4.4.1;Using Temp Data;556
6.4.5.5;Returning Different Types of Content;557
6.4.5.5.1;Producing a JSON Response;557
6.4.5.5.2;Using Objects to Generate Responses;558
6.4.5.6;Responding with the Contents of Files;559
6.4.5.7;Returning Errors and HTTP Codes;561
6.4.5.7.1;Sending a Specific HTTP Result Code;562
6.4.5.7.2;Sending a 404 Result Using a Convenience Class;563
6.4.5.8;Understanding the Other Action Result Classes;563
6.4.6;Summary;564
6.5;Chapter 18: Dependency Injection;565
6.5.1;Preparing the Example Project;566
6.5.1.1;Creating the Model and Repository;567
6.5.1.2;Creating the Controller and View;569
6.5.1.3;Creating the Unit Test Project;571
6.5.2;Creating Loosely Coupled Components;572
6.5.2.1;Examining Closely Coupled Components;572
6.5.2.1.1;De-coupling Components for Unit Testing;573
6.5.2.1.2;Using a Type Broker;575
6.5.3;Introducing ASP.NET Dependency Injection;579
6.5.3.1;Preparing for Dependency Injection;579
6.5.3.2;Configuring the Service Provider;580
6.5.3.3;Unit Testing a Controller with a Dependency;582
6.5.3.4;Using Dependency Chains;583
6.5.3.5;Using Dependency Injection for Concrete Types;586
6.5.4;Understanding Service Life Cycles;588
6.5.4.1;Using the Transient Life Cycle;588
6.5.4.1.1;Using a Factory Function;591
6.5.4.2;Using the Scoped Life Cycle;592
6.5.4.3;Using the Singleton Life Cycle;594
6.5.5;Using Action Injection;595
6.5.6;Using the Property Injection Attributes;595
6.5.7;Manually Requesting an Implementation Object;596
6.5.8;Summary;597
6.6;Chapter 19: Filters;598
6.6.1;Preparing the Example Project;599
6.6.1.1;Enabling SSL;600
6.6.1.2;Creating the Controller and View;601
6.6.2;Using Filters;603
6.6.3;Understanding Filters;606
6.6.3.1;Getting Context Data;606
6.6.4;Using Authorization Filters;607
6.6.4.1;Creating an Authorization Filter;608
6.6.5;Using Action Filters;610
6.6.5.1;Creating an Action Filter;612
6.6.5.2;Creating an Asynchronous Action Filter;614
6.6.6;Using Result Filters;615
6.6.6.1;Creating a Result Filter;616
6.6.6.2;Creating an Asynchronous Result Filter;617
6.6.6.3;Creating a Hybrid Action/Result Filter;619
6.6.7;Using Exception Filters;621
6.6.7.1;Creating an Exception Filter;622
6.6.8;Using Dependency Injection for Filters;624
6.6.8.1;Resolving Filter Dependencies;624
6.6.8.1.1;Creating Filters with Dependencies;625
6.6.8.1.2;Applying the Filters;627
6.6.8.2;Managing Filter Life Cycles;628
6.6.8.2.1;Applying the Filter;630
6.6.9;Creating Global Filters;631
6.6.10;Understanding and Changing Filter Order;634
6.6.10.1;Changing Filter Order;636
6.6.11;Summary;637
6.7;Chapter 20: API Controllers;638
6.7.1;Preparing the Example Project;639
6.7.1.1;Creating the Model and Repository;639
6.7.1.2;Creating the Controller and Views;641
6.7.1.3;Configuring the Application;643
6.7.1.3.1;Setting the HTTP Port;644
6.7.2;Understanding the Role of RESTful Controllers;645
6.7.2.1;Understanding the Speed Problem;646
6.7.2.2;Understanding the Efficiency Problem;646
6.7.2.3;Understanding the Openness Problem;647
6.7.3;Introducing REST and API Controllers;647
6.7.3.1;Creating an API Controller;648
6.7.3.1.1;Defining the Route;650
6.7.3.1.2;Declaring Dependencies;650
6.7.3.1.3;Defining the Action Methods;650
6.7.3.1.4;Defining the Action Results;651
6.7.3.2;Testing an API Controller;652
6.7.3.2.1;Testing the GET Operations;653
6.7.3.2.2;Testing the POST Operation;654
6.7.3.2.3;Testing the PUT Operation;655
6.7.3.2.4;Testing the Delete Operation;655
6.7.3.3;Using the API Controller in the Browser;656
6.7.4;Understanding Content Formatting;658
6.7.4.1;Understanding the Default Content Policy;659
6.7.4.2;Understanding Content Negotiation;660
6.7.4.2.1;Enabling XML Formatting;661
6.7.4.3;Specifying an Action Data Format;663
6.7.4.4;Getting the Data Format from the Route or Query String;664
6.7.4.5;Enabling Full Content Negotiation;665
6.7.4.6;Receiving Different Data Formats;667
6.7.5;Summary;668
6.8;Chapter 21: Views;669
6.8.1;Preparing the Example Project;670
6.8.2;Creating a Custom View Engine;672
6.8.2.1;Creating a Custom IView;673
6.8.2.2;Creating an IViewEngine Implementation;674
6.8.2.3;Registering a Custom View Engine;675
6.8.2.4;Testing the View Engine;676
6.8.3;Working with the Razor Engine;679
6.8.3.1;Preparing the Example Project;679
6.8.3.2;Demystifying Razor Views;681
6.8.3.2.1;Understanding the Class Name;682
6.8.3.2.2;Understanding the Base Class;683
6.8.3.2.3;Understanding the View Rendering;684
6.8.4;Adding Dynamic Content to a Razor View;685
6.8.4.1;Using Layout Sections;685
6.8.4.1.1;Testing for Sections;688
6.8.4.1.2;Rendering Optional Sections;689
6.8.4.2;Using Partial Views;691
6.8.4.2.1;Creating a Partial View;691
6.8.4.2.2;Applying a Partial View;691
6.8.4.2.3;Using Strongly Typed Partial Views;692
6.8.4.3;Adding JSON Content to Views;694
6.8.5;Configuring Razor;696
6.8.5.1;Understanding View Location Expanders;697
6.8.5.1.1;Creating a Simple View Location Expander;697
6.8.5.1.1.1;Applying the View Location Expander;698
6.8.5.1.2;Selecting Specific Views for Requests;699
6.8.6;Summary;702
6.9;Chapter 22: View Components;703
6.9.1;Preparing the Example Project;704
6.9.1.1;Creating the Models and Repositories;705
6.9.1.2;Creating the Controller and Views;707
6.9.1.3;Configuring the Application;710
6.9.2;Understanding View Components;711
6.9.3;Creating a View Component;712
6.9.3.1;Creating POCO View Components;712
6.9.3.2;Deriving from the ViewComponent Base Class;714
6.9.3.3;Understanding View Component Results;715
6.9.3.3.1;Returning a Partial View;716
6.9.3.3.2;Returning HTML Fragments;719
6.9.3.4;Getting Context Data;721
6.9.3.4.1;Providing Context from the Parent View Using Arguments;723
6.9.3.5;Creating Asynchronous View Components;727
6.9.4;Creating Hybrid Controller/View Component Classes;730
6.9.4.1;Creating the Hybrid Views;731
6.9.4.2;Applying the Hybrid Class;732
6.9.5;Summary;734
6.10;Chapter 23: Understanding Tag Helpers;735
6.10.1;Preparing the Example Project;736
6.10.1.1;Creating the Model and Repository;737
6.10.1.2;Creating the Controller, Layout, and Views;738
6.10.1.3;Configuring the Application;741
6.10.2;Creating a Tag Helper;742
6.10.2.1;Defining the Tag Helper Class;742
6.10.2.1.1;Receiving Context Data;743
6.10.2.1.2;Producing Output;744
6.10.2.2;Registering Tag Helpers;745
6.10.2.3;Using a Tag Helper;746
6.10.2.4;Managing the Scope of a Tag Helper;748
6.10.2.4.1;Narrowing the Scope of a Tag Helper;749
6.10.2.4.2;Widening the Scope of a Tag Helper;750
6.10.3;Advanced Tag Helper Features;752
6.10.3.1;Creating Shorthand Elements;752
6.10.3.2;Prepending and Appending Content and Elements;755
6.10.3.2.1;Inserting Content Around the Output Element;755
6.10.3.2.2;Inserting Content Inside the Output Element;757
6.10.3.3;Getting View Context Data and Using Dependency Injection;759
6.10.3.4;Working with the View Model;761
6.10.3.5;Coordinating Between Tag Helpers;763
6.10.3.6;Suppressing the Output Element;765
6.10.4;Summary;767
6.11;Chapter 24: Using the Form Tag Helpers;768
6.11.1;Preparing the Example Project;769
6.11.1.1;Changing the Tag Helper Registration;769
6.11.1.2;Resetting the Views and Layout;770
6.11.2;Working with Form Elements;772
6.11.2.1;Setting the Form Target;772
6.11.2.2;Using the Anti-forgery Feature;773
6.11.3;Working with Input Elements;775
6.11.3.1;Configuring Input Elements;776
6.11.3.2;Formatting Data Values;778
6.11.3.2.1;Applying Formatting via the Model Class;780
6.11.4;Working with Label Elements;781
6.11.5;Working with Select and Option Elements;783
6.11.5.1;Using a Data Source to Populate a select Element;785
6.11.5.2;Generating Option Elements from an enum;785
6.11.5.2.1;Generating Option Elements from the Model;786
6.11.5.2.2;Using a Custom Tag Helper to Generate Option Elements from the Model;788
6.11.6;Working with Text Areas;790
6.11.7;Understanding the Validation Form Tag Helpers;792
6.11.8;Summary;792
6.12;Chapter 25: Using the Other Built-in Tag Helpers;793
6.12.1;Preparing the Example Project;794
6.12.2;Using the Hosting Environment Tag Helper;795
6.12.3;Using the JavaScript and CSS Tag Helpers;796
6.12.3.1;Managing JavaScript Files;796
6.12.3.1.1;Selecting JavaScript Files;797
6.12.3.1.1.1;Narrowing the Globbing Pattern;800
6.12.3.1.1.2;Excluding Files;800
6.12.3.1.1.3;Using the Hosting Environment to Select Files;801
6.12.3.1.2;Working with Content Delivery Networks;803
6.12.3.2;Managing CSS Stylesheets;805
6.12.3.2.1;Selecting Stylesheets;805
6.12.3.2.2;Working with Content Delivery Networks;807
6.12.4;Working with Anchor Elements;808
6.12.5;Working with Image Elements;809
6.12.6;Using the Data Cache;810
6.12.6.1;Setting Cache Expiry;813
6.12.6.1.1;Setting a Fixed Expiry Point;813
6.12.6.1.2;Setting a Last-Used Expiry Period;814
6.12.6.2;Using Cache Variations;814
6.12.7;Using Application-Relative URLs;815
6.12.8;Summary;818
6.13;Chapter 26: Model Binding;819
6.13.1;Preparing the Example Project;820
6.13.1.1;Creating the Model and Repository;821
6.13.1.2;Creating the Controller and View;822
6.13.1.3;Configuring the Application;824
6.13.2;Understanding Model Binding;825
6.13.2.1;Understanding Default Binding Values;827
6.13.2.2;Binding Simple Types;829
6.13.2.3;Binding Complex Types;830
6.13.2.3.1;Creating Easily Bound HTML;832
6.13.2.3.2;Specifying Custom Prefixes;835
6.13.2.3.3;Selectively Binding Properties;839
6.13.2.4;Binding to Arrays and Collections;841
6.13.2.4.1;Binding to Arrays;841
6.13.2.4.2;Binding to Collections;843
6.13.2.4.3;Binding to Collections of Complex Types;844
6.13.3;Specifying a Model Binding Source;848
6.13.3.1;Selecting a Standard Binding Source;849
6.13.3.2;Using Headers As Binding Sources;850
6.13.3.2.1;Binding Complex Types from Headers;852
6.13.3.3;Using Request Bodies as Binding Sources;853
6.13.4;Summary;856
6.14;Chapter 27: Model Validation;857
6.14.1;Preparing the Example Project;858
6.14.1.1;Creating the Model;860
6.14.1.2;Creating the Controller;860
6.14.1.3;Creating the Layout and Views;861
6.14.2;Understanding the Need for Model Validation;863
6.14.3;Explicitly Validating a Model;864
6.14.3.1;Displaying Validation Errors to the User;866
6.14.3.2;Displaying Validation Messages;869
6.14.3.2.1;Configuring the Default Validation Error Messages;871
6.14.3.3;Displaying Property-Level Validation Messages;873
6.14.3.4;Displaying Model-Level Messages;875
6.14.4;Specifying Validation Rules Using Metadata;878
6.14.4.1;Creating a Custom Property Validation Attribute;882
6.14.5;Performing Client-Side Validation;884
6.14.6;Performing Remote Validation;886
6.14.7;Summary;890
6.15;Chapter 28: Getting Started with Identity;891
6.15.1;Preparing the Example Project;892
6.15.1.1;Creating the Controller and View;894
6.15.2;Setting Up ASP.NET Core Identity;896
6.15.2.1;Adding the Identity Package to the Application;896
6.15.2.2;Creating the User Class;897
6.15.2.2.1;Configuring the View Imports;898
6.15.2.3;Creating the Database Context Class;899
6.15.2.4;Configuring the Database Connection String Setting;899
6.15.2.5;Configuring the Identity Services and Middleware;901
6.15.2.6;Creating the Identity Database;902
6.15.3;Using ASP.NET Core Identity;903
6.15.3.1;Enumerating User Accounts;903
6.15.3.2;Creating Users;906
6.15.3.2.1;Testing the Create Functionality;909
6.15.3.3;Validating Passwords;910
6.15.3.3.1;Implementing a Custom Password Validator;913
6.15.3.4;Validating User Details;918
6.15.3.4.1;Implementing Custom User Validation;921
6.15.4;Completing the Administration Features;924
6.15.4.1;Implementing the Delete Feature;925
6.15.4.2;Implementing the Edit Feature;926
6.15.4.2.1;Creating the View;929
6.15.5;Summary;931
6.16;Chapter 29: Applying ASP.NET Core Identity;932
6.16.1;Preparing the Example Project;932
6.16.2;Authenticating Users;933
6.16.2.1;Preparing to Implement Authentication;936
6.16.2.2;Adding User Authentication;939
6.16.2.3;Testing Authentication;941
6.16.3;Authorizing Users with Roles;942
6.16.3.1;Creating and Deleting Roles;943
6.16.3.1.1;Creating the Views;945
6.16.3.1.2;Testing, Creating, and Deleting Roles;948
6.16.3.2;Managing Role Memberships;948
6.16.3.2.1;Testing and Editing Role Membership;952
6.16.3.3;Using Roles for Authorization;954
6.16.4;Seeding the Database;958
6.16.5;Summary;961
6.17;Chapter 30: Advanced ASP.NET Core Identity;962
6.17.1;Preparing the Example Project;962
6.17.2;Adding Custom User Properties;964
6.17.2.1;Preparing for Database Migration;967
6.17.2.2;Testing the Custom Properties;968
6.17.3;Working with Claims and Policies;969
6.17.3.1;Understanding Claims;969
6.17.3.2;Creating Claims;974
6.17.3.3;Using Policies;977
6.17.3.3.1;Creating Custom Policy Requirements;980
6.17.3.4;Using Policies to Authorize Access to Resources;983
6.17.3.4.1;Creating the Resource Authorization Policy and Handler;986
6.17.4;Using Third-Party Authentication;989
6.17.4.1;Registering the Application with Google;989
6.17.4.2;Enabling Google Authentication;990
6.17.5;Summary;995
6.18;Chapter 31: Model Conventions and Action Constraints;996
6.18.1;Preparing the Example Project;996
6.18.1.1;Creating the View Model, Controller, and View;998
6.18.2;Using the Application Model and Model Conventions;1000
6.18.2.1;Understanding the Application Model;1001
6.18.2.1.1;Customizing the Application Model;1003
6.18.2.2;Understanding the Role of Model Conventions;1005
6.18.2.3;Creating a Model Convention;1006
6.18.2.3.1;Using Conventions That Add or Remove Models;1007
6.18.2.4;Understanding Model Convention Execution Order;1011
6.18.2.5;Creating Global Model Conventions;1012
6.18.3;Using Action Constraints;1014
6.18.3.1;Preparing the Example Project;1014
6.18.3.2;Understanding Action Constraints;1016
6.18.3.3;Creating an Action Constraint;1017
6.18.3.3.1;Creating a Comparative Action Constraint;1019
6.18.3.4;Resolving Dependencies in Action Constraints;1022
6.18.4;Summary;1025
7;Index;1026




