Previous Next

Pro ASP.NET Core MVC 2 - Seventh Edition Develop cloud-ready web applications using Microsoft’s latest framework, ASP.NET Core… (Adam Freeman) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Adam Freeman

C/C++/C#

Now in its 7th edition, the best selling book on MVC is updated for ASP.NET Core MVC 2. It contains detailed explanations of the 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 as Web 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. The book includes a fully worked case study of a functioning web application that readers can use as a template for their own projects. What's New in This Edition Fully updated for Visual Studio 2017, C# 7 and .NET Core 2 Coverage of new features such as view filters Wider platform and tooling coverage than ever before, with more on Visual Studio Code and working with .NET Core on non-Windows platforms Docker-based application deployment 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 See how MVC and test-driven development work in action Learn what's new in ASP.NET Core MVC 2 and h

📄 File Format: PDF
💾 File Size: 22.0 MB
4
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
Pro ASP.NET Core MVC 2 Develop cloud-ready web applications using Microsof t’s latest framework, ASP.NET Core MVC 2 — Seventh Edition — Adam Freeman
📄 Page 2
Pro ASP.NET Core MVC 2 Seventh Edition Adam Freeman
📄 Page 3
Pro ASP.NET Core MVC 2 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.com) 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
📄 Page 4
Dedicated to my lovely wife, Jacqui Griffyth. (And also to Peanut.)
📄 Page 5
v Contents at a Glance About the Author �������������������������������������������������������������������������������������������������xxvii About the Technical Reviewer ������������������������������������������������������������������������������xxix ■Part I: Introducing ASP�NET Core MVC 2 ��������������������������������������������� 1 ■Chapter 1: ASP �NET Core MVC in Context ������������������������������������������������������������� 3 ■Chapter 2: Your First MVC Application ���������������������������������������������������������������� 11 ■Chapter 3: The MVC Pattern, Projects, and Conventions ������������������������������������� 53 ■Chapter 4: Essential C# Features ������������������������������������������������������������������������� 67 ■Chapter 5: Working with Razor ������������������������������������������������������������������������� 105 ■Chapter 6: Working with Visual Studio �������������������������������������������������������������� 127 ■Chapter 7: Unit Testing MVC Applications ��������������������������������������������������������� 163 ■Chapter 8: SportsStore: A Real Application ������������������������������������������������������� 193 ■Chapter 9: SportsStore: Navigation ������������������������������������������������������������������� 237 ■Chapter 10: SportsStore: Completing the Cart �������������������������������������������������� 271 ■Chapter 11: SportsStore: Administration ���������������������������������������������������������� 295 ■Chapter 12: SportsStore: Security and Deployment ������������������������������������������ 323 ■Chapter 13: Working with Visual Studio Code ��������������������������������������������������� 349 ■Part II: ASP�NET Core MVC 2 in Detail ��������������������������������������������� 373 ■Chapter 14: Configuring Applications ���������������������������������������������������������������� 375 ■Chapter 15: URL Routing������������������������������������������������������������������������������������ 429 ■Chapter 16: Advanced Routing Features ����������������������������������������������������������� 469
📄 Page 6
■ Contents at a GlanCe vi ■Chapter 17: Controllers and Actions ������������������������������������������������������������������ 507 ■Chapter 18: Dependency Injection ��������������������������������������������������������������������� 551 ■Chapter 19: Filters ��������������������������������������������������������������������������������������������� 585 ■Chapter 20: API Controllers ������������������������������������������������������������������������������� 625 ■Chapter 21: Views ��������������������������������������������������������������������������������������������� 657 ■Chapter 22: View Components �������������������������������������������������������������������������� 691 ■Chapter 23: Understanding Tag Helpers ������������������������������������������������������������ 721 ■Chapter 24: Using the Form Tag Helpers ����������������������������������������������������������� 755 ■Chapter 25: Using the Other Built-in Tag Helpers ���������������������������������������������� 781 ■Chapter 26: Model Binding �������������������������������������������������������������������������������� 811 ■Chapter 27: Model Validation ���������������������������������������������������������������������������� 849 ■Chapter 28: Getting Started with Identity ���������������������������������������������������������� 881 ■Chapter 29: Applying ASP�NET Core Identity ������������������������������������������������������ 919 ■Chapter 30: Advanced ASP�NET Core Identity ���������������������������������������������������� 949 ■Chapter 31: Model Conventions and Action Constraints ����������������������������������� 983 Index ������������������������������������������������������������������������������������������������������������������� 1013
📄 Page 7
vii Contents About the Author �������������������������������������������������������������������������������������������������xxvii About the Technical Reviewer ������������������������������������������������������������������������������xxix ■Part I: Introducing ASP�NET Core MVC 2 ��������������������������������������������� 1 ■Chapter 1: ASP �NET Core MVC in Context ������������������������������������������������������������� 3 Understanding the History of ASP.NET Core MVC ............................................................. 3 ASP.NET Web Forms ................................................................................................................................ 4 The Original MVC Framework ................................................................................................................. 5 Understanding ASP.NET Core ............................................................................................ 5 What’s New in ASP.NET Core MVC 2 ....................................................................................................... 6 Key Benefits of ASP.NET Core MVC ......................................................................................................... 6 What Do I Need to Know? ................................................................................................. 9 What Is the Structure of This Book? ................................................................................. 9 Part 1: Introducing ASP.NET Core MVC .................................................................................................... 9 Part 2: ASP.NET Core MVC in Detail ......................................................................................................... 9 Where Can You Get the Example Code? ........................................................................... 9 Where Can You Get Corrections for This Book? .............................................................. 10 Contacting the Author ..................................................................................................... 10 Summary ........................................................................................................................ 10 ■Chapter 2: Your First MVC Application ���������������������������������������������������������������� 11 Installing Visual Studio ................................................................................................... 11 Installing the .NET Core 2.0 SDK .................................................................................... 12
📄 Page 8
■ Contents viii Creating a New ASP.NET Core MVC Project .................................................................... 13 Adding the Controller ............................................................................................................................ 17 Understanding Routes .......................................................................................................................... 20 Rendering Web Pages .................................................................................................... 20 Creating and Rendering a View ............................................................................................................ 20 Adding Dynamic Output ........................................................................................................................ 23 Creating a Simple Data-Entry Application ...................................................................... 25 Setting the Scene ................................................................................................................................. 25 Designing a Data Model ........................................................................................................................ 26 Creating a Second Action and a Strongly Typed View ........................................................................... 27 Linking Action Methods ........................................................................................................................ 29 Building the Form ................................................................................................................................. 30 Receiving Form Data ............................................................................................................................ 32 Displaying the Responses..................................................................................................................... 37 Adding Validation .................................................................................................................................. 39 Styling the Content ............................................................................................................................... 45 Summary ........................................................................................................................ 51 ■Chapter 3: The MVC Pattern, Projects, and Conventions ������������������������������������� 53 The History of MVC ......................................................................................................... 53 Understanding the MVC Pattern ..................................................................................... 53 Understanding Models .......................................................................................................................... 53 Understanding Controllers .................................................................................................................... 54 Understanding Views ............................................................................................................................ 55 The ASP.NET Implementation of MVC ................................................................................................... 55 Comparing MVC to Other Patterns.................................................................................. 56 Understanding the Smart UI Pattern ..................................................................................................... 56 Understanding the Model-View Architecture ........................................................................................ 57 Understanding Classic Three-Tier Architecture .................................................................................... 58 Understanding Variations on MVC ........................................................................................................ 59
📄 Page 9
■ Contents ix Understanding ASP.NET Core MVC Projects .................................................................... 60 Creating the Project .............................................................................................................................. 60 Understanding MVC Conventions ......................................................................................................... 63 Summary ........................................................................................................................ 65 ■Chapter 4: Essential C# Features ������������������������������������������������������������������������� 67 Preparing the Example Project ....................................................................................... 68 Enabling ASP.NET Core MVC ................................................................................................................. 69 Creating the MVC Application Components .......................................................................................... 70 Using the Null Conditional Operator ............................................................................... 73 Chaining the Null Conditional Operator ................................................................................................ 74 Combining the Conditional and Coalescing Operators ......................................................................... 75 Using Automatically Implemented Properties ................................................................ 76 Using Auto-Implemented Property Initializers ...................................................................................... 77 Creating Read-Only Automatically Implemented Properties ................................................................. 78 Using String Interpolation ............................................................................................... 79 Using Object and Collection Initializers .......................................................................... 80 Using an Index Initializer ...................................................................................................................... 82 Pattern Matching ............................................................................................................ 83 Pattern Matching in Switch Statements ............................................................................................... 84 Using Extension Methods ............................................................................................... 85 Applying Extension Methods to an Interface ........................................................................................ 86 Creating Filtering Extension Methods ................................................................................................... 88 Using Lambda Expressions ............................................................................................ 90 Defining Functions ................................................................................................................................ 91 Using Lambda Expression Methods and Properties ............................................................................. 94 Using Type Inference and Anonymous Types .................................................................. 96 Using Anonymous Types ....................................................................................................................... 97 Using Asynchronous Methods ........................................................................................ 99 Working with Tasks Directly ................................................................................................................. 99 Applying the async and await Keywords ............................................................................................ 101 Getting Names .............................................................................................................. 102 Summary ...................................................................................................................... 104
📄 Page 10
■ Contents x ■Chapter 5: Working with Razor ������������������������������������������������������������������������� 105 Preparing the Example Project ..................................................................................... 106 Defining the Model ............................................................................................................................. 107 Creating the Controller ....................................................................................................................... 107 Creating the View ............................................................................................................................... 108 Working with the Model Object .................................................................................... 109 Using View Imports ............................................................................................................................. 111 Working with Layouts ................................................................................................... 112 Creating the Layout ............................................................................................................................ 113 Applying a Layout ............................................................................................................................... 115 Using a View Start File ........................................................................................................................ 116 Using Razor Expressions .............................................................................................. 118 Inserting Data Values .......................................................................................................................... 119 Setting Attribute Values ...................................................................................................................... 121 Using Conditional Statements ............................................................................................................ 122 Enumerating Arrays and Collections ................................................................................................... 124 Summary ...................................................................................................................... 126 ■Chapter 6: Working with Visual Studio �������������������������������������������������������������� 127 Preparing the Example Project ..................................................................................... 127 Creating the Model ............................................................................................................................. 128 Creating the Controller and View ........................................................................................................ 129 Managing Software Packages ...................................................................................... 131 Understanding NuGet.......................................................................................................................... 131 Understanding Bower ......................................................................................................................... 133 Understanding Iterative Development .......................................................................... 137 Making Changes to Razor Views ........................................................................................................ 138 Making Changes to C# Classes .......................................................................................................... 139 Using Browser Link ............................................................................................................................. 147
📄 Page 11
■ Contents xi Preparing JavaScript and CSS for Deployment ............................................................ 152 Enabling Static Content Delivery ........................................................................................................ 153 Adding Static Content to the Project................................................................................................... 153 Updating the View ............................................................................................................................... 156 Bundling and Minifying in MVC Applications ...................................................................................... 157 Summary ...................................................................................................................... 161 ■Chapter 7: Unit Testing MVC Applications ��������������������������������������������������������� 163 Preparing the Example Project ..................................................................................... 164 Enabling the Built-in Tag Helpers ....................................................................................................... 164 Adding Actions to the Controller ......................................................................................................... 164 Creating the Data Entry Form ............................................................................................................. 165 Updating the Index View ..................................................................................................................... 166 Unit Testing MVC Applications ...................................................................................... 167 Creating a Unit Test Project ................................................................................................................ 168 Creating the Project Reference ........................................................................................................... 169 Writing and Running Unit Tests ........................................................................................................... 169 Isolating Components for Unit Testing ................................................................................................ 174 Improving Unit Tests ..................................................................................................... 182 Parameterizing a Unit Test .................................................................................................................. 182 Improving Fake Implementations ....................................................................................................... 186 Summary ...................................................................................................................... 191 ■Chapter 8: SportsStore: A Real Application ������������������������������������������������������� 193 Getting Started ............................................................................................................. 194 Creating the MVC Project .................................................................................................................... 194 Creating the Unit Test Project ............................................................................................................. 198 Checking and Running the Application ............................................................................................... 199 Starting the Domain Model........................................................................................... 200 Creating a Repository ......................................................................................................................... 201 Creating a Fake Repository ................................................................................................................ 202 Registering the Repository Service .................................................................................................... 202
📄 Page 12
■ Contents xii Displaying a List of Products ........................................................................................ 203 Adding a Controller ............................................................................................................................. 204 Adding and Configuring the View ....................................................................................................... 205 Setting the Default Route ................................................................................................................... 207 Running the Application ...................................................................................................................... 208 Preparing a Database ................................................................................................... 208 Installing the Entity Framework Core Tools Package .......................................................................... 209 Creating the Database Classes ........................................................................................................... 210 Creating the Repository Class ............................................................................................................ 210 Defining the Connection String ........................................................................................................... 211 Configuring the Application ................................................................................................................ 212 Creating the Database Migration ........................................................................................................ 214 Creating the Seed Data ....................................................................................................................... 215 Adding Pagination ........................................................................................................ 218 Displaying Page Links......................................................................................................................... 220 Improving the URLs ............................................................................................................................ 229 Styling the Content ....................................................................................................... 231 Installing the Bootstrap Package ........................................................................................................ 231 Applying Bootstrap Styles to the Layout ............................................................................................. 232 Creating a Partial View ....................................................................................................................... 235 Summary ...................................................................................................................... 236 ■Chapter 9: SportsStore: Navigation ������������������������������������������������������������������� 237 Adding Navigation Controls .......................................................................................... 237 Filtering the Product List .................................................................................................................... 237 Refining the URL Scheme ................................................................................................................... 242 Building a Category Navigation Menu ................................................................................................ 246 Correcting the Page Count .................................................................................................................. 253 Building the Shopping Cart........................................................................................... 257 Defining the Cart Model ...................................................................................................................... 257 Adding the Add to Cart Buttons .......................................................................................................... 261 Enabling Sessions .............................................................................................................................. 263
📄 Page 13
■ Contents xiii Implementing the Cart Controller ....................................................................................................... 264 Displaying the Contents of the Cart .................................................................................................... 266 Summary ...................................................................................................................... 270 ■Chapter 10: SportsStore: Completing the Cart �������������������������������������������������� 271 Refining the Cart Model with a Service ........................................................................ 271 Creating a Storage-Aware Cart Class ................................................................................................. 271 Registering the Service ...................................................................................................................... 272 Simplifying the Cart Controller ........................................................................................................... 273 Completing the Cart Functionality ................................................................................ 274 Removing Items from the Cart ............................................................................................................ 274 Adding the Cart Summary Widget ...................................................................................................... 276 Submitting Orders ........................................................................................................ 279 Creating the Model Class .................................................................................................................... 279 Adding the Checkout Process ............................................................................................................. 280 Implementing Order Processing ......................................................................................................... 284 Completing the Order Controller ......................................................................................................... 287 Displaying Validation Errors ................................................................................................................ 290 Displaying a Summary Page ............................................................................................................... 292 Summary ...................................................................................................................... 293 ■Chapter 11: SportsStore: Administration ���������������������������������������������������������� 295 Managing Orders .......................................................................................................... 295 Enhancing the Model .......................................................................................................................... 295 Adding the Actions and View .............................................................................................................. 296 Adding Catalog Management ....................................................................................... 299 Creating a CRUD Controller ................................................................................................................. 300 Implementing the List View ................................................................................................................ 302 Editing Products ................................................................................................................................. 303 Creating New Products ....................................................................................................................... 317 Deleting Products ............................................................................................................................... 318 Summary ...................................................................................................................... 322
📄 Page 14
■ Contents xiv ■Chapter 12: SportsStore: Security and Deployment ������������������������������������������ 323 Securing the Administration Features .......................................................................... 323 Creating the Identity Database ........................................................................................................... 323 Applying a Basic Authorization Policy ................................................................................................. 328 Creating the Account Controller and Views ........................................................................................ 330 Testing the Security Policy.................................................................................................................. 334 Deploying the Application ............................................................................................. 334 Creating the Databases ...................................................................................................................... 334 Preparing the Application ................................................................................................................... 336 Applying the Database Migrations ...................................................................................................... 340 Managing Database Seeding .............................................................................................................. 340 Deploying the Application ................................................................................................................... 344 Summary ...................................................................................................................... 348 ■Chapter 13: Working with Visual Studio Code ��������������������������������������������������� 349 Setting Up the Development Environment ................................................................... 349 Installing Node.js ................................................................................................................................ 349 Checking the Node Installation ........................................................................................................... 351 Installing Git ........................................................................................................................................ 351 Checking the Git Installation ............................................................................................................... 351 Installing Bower .................................................................................................................................. 352 Installing .NET Core ............................................................................................................................ 352 Checking the .NET Core Installation ................................................................................................... 353 Installing Visual Studio Code .............................................................................................................. 353 Checking the Visual Studio Code Installation ..................................................................................... 354 Installing the Visual Studio Code C# Extension ................................................................................... 355 Creating an ASP.NET Core Project ................................................................................ 355 Preparing the Project with Visual Studio Code ............................................................. 356 Managing Client-Side Packages ......................................................................................................... 357 Configuring the Application ................................................................................................................ 359 Building and Running the Project ....................................................................................................... 359
📄 Page 15
■ Contents xv Re-creating the PartyInvites Application ...................................................................... 360 Creating the Model and Repository .................................................................................................... 360 Creating the Database ........................................................................................................................ 364 Creating the Controllers and Views .................................................................................................... 366 Unit Testing in Visual Studio Code ................................................................................ 370 Creating a Unit Test ............................................................................................................................. 371 Running Tests ..................................................................................................................................... 371 Summary ...................................................................................................................... 372 ■Part II: ASP�NET Core MVC 2 in Detail ��������������������������������������������� 373 ■Chapter 14: Configuring Applications ���������������������������������������������������������������� 375 Preparing the Example Project ..................................................................................... 376 Configuring the Project................................................................................................. 378 Adding Packages to the Project .......................................................................................................... 379 Adding Tools Packages to the Project ................................................................................................. 381 Understanding the Program Class ................................................................................ 382 Digging into the Configuration Detail .................................................................................................. 382 Understanding the Startup Class .................................................................................. 386 Understanding ASP.NET Services ........................................................................................................ 388 Understanding ASP.NET Middleware ................................................................................................... 391 Understanding How the Configure Method Is Invoked ....................................................................... 400 Adding the Remaining Middleware Components ................................................................................ 405 Configuring the Application .......................................................................................... 409 Creating the JSON Configuration File ................................................................................................. 411 Using Configuration Data .................................................................................................................... 413 Configuring Logging ........................................................................................................................... 415 Configuring Dependency Injection ...................................................................................................... 419 Configuring MVC Services ............................................................................................ 420 Dealing with Complex Configurations .......................................................................... 422 Creating Different External Configuration Files .................................................................................. 422 Creating Different Configuration Methods .......................................................................................... 424 Creating Different Configuration Classes ............................................................................................ 425 Summary ...................................................................................................................... 427
📄 Page 16
■ Contents xvi ■Chapter 15: URL Routing������������������������������������������������������������������������������������ 429 Preparing the Example Project ..................................................................................... 430 Creating the Model Class .................................................................................................................... 431 Creating the Example Controllers ....................................................................................................... 431 Creating the View ............................................................................................................................... 432 Introducing URL Patterns ............................................................................................. 434 Creating and Registering a Simple Route .................................................................... 435 Defining Default Values ................................................................................................ 437 Defining Inline Default Values ............................................................................................................. 438 Using Static URL Segments .......................................................................................... 440 Defining Custom Segment Variables ............................................................................ 445 Using Custom Variables as Action Method Parameters ...................................................................... 448 Defining Optional URL Segments ........................................................................................................ 449 Defining Variable-Length Routes ........................................................................................................ 451 Constraining Routes ..................................................................................................... 453 Constraining a Route Using a Regular Expression .............................................................................. 457 Using Type and Value Constraints ....................................................................................................... 458 Combining Constraints ....................................................................................................................... 459 Defining a Custom Constraint ............................................................................................................. 461 Using Attribute Routing ................................................................................................ 463 Preparing for Attribute Routing ........................................................................................................... 463 Applying Attribute Routing .................................................................................................................. 464 Applying Route Constraints ................................................................................................................ 467 Summary ...................................................................................................................... 468 ■Chapter 16: Advanced Routing Features ����������������������������������������������������������� 469 Preparing the Example Project ..................................................................................... 470 Generating Outgoing URLs in Views ............................................................................. 471 Generating Outgoing Links ................................................................................................................. 471 Generating URLs (and Not Links) ........................................................................................................ 483
📄 Page 17
■ Contents xvii Customizing the Routing System ................................................................................. 484 Changing the Routing System Configuration ...................................................................................... 484 Creating a Custom Route Class .......................................................................................................... 486 Working with Areas ...................................................................................................... 497 Creating an Area ................................................................................................................................. 497 Creating an Area Route ....................................................................................................................... 498 Populating an Area ............................................................................................................................. 499 Generating Links to Actions in Areas .................................................................................................. 502 URL Schema Best Practices ......................................................................................... 503 Making Your URLs Clean and Human-Friendly ................................................................................... 503 GET and POST: Picking the Right One ................................................................................................. 504 Summary ...................................................................................................................... 505 ■Chapter 17: Controllers and Actions ������������������������������������������������������������������ 507 Preparing the Example Project ..................................................................................... 508 Preparing the Views ............................................................................................................................ 509 Understanding Controllers ............................................................................................ 511 Creating Controllers ..................................................................................................... 512 Creating POCO Controllers .................................................................................................................. 512 Using the Controller Base Class ......................................................................................................... 514 Receiving Context Data ................................................................................................ 516 Getting Data from Context Objects ..................................................................................................... 516 Using Action Method Parameters ....................................................................................................... 520 Producing a Response .................................................................................................. 522 Producing a Response Using the Context Object ................................................................................ 522 Understanding Action Results ............................................................................................................. 524 Producing an HTML Response ............................................................................................................ 526 Performing Redirections ..................................................................................................................... 535 Returning Different Types of Content .................................................................................................. 543 Responding with the Contents of Files ............................................................................................... 545
📄 Page 18
■ Contents xviii Returning Errors and HTTP Codes ...................................................................................................... 547 Understanding the Other Action Result Classes ................................................................................. 549 Summary ...................................................................................................................... 550 ■Chapter 18: Dependency Injection ��������������������������������������������������������������������� 551 Preparing the Example Project ..................................................................................... 552 Creating the Model and Repository .................................................................................................... 553 Creating the Controller and View ........................................................................................................ 554 Creating the Unit Test Project ............................................................................................................. 556 Creating Loosely Coupled Components ........................................................................ 556 Examining Closely Coupled Components............................................................................................ 557 Introducing ASP.NET Dependency Injection .................................................................. 564 Preparing for Dependency Injection ................................................................................................... 564 Configuring the Service Provider ........................................................................................................ 565 Unit Testing a Controller with a Dependency ...................................................................................... 567 Using Dependency Chains .................................................................................................................. 568 Using Dependency Injection for Concrete Types ................................................................................. 570 Understanding Service Life Cycles ............................................................................... 573 Using the Transient Life Cycle............................................................................................................. 574 Using the Scoped Life Cycle ............................................................................................................... 578 Using the Singleton Life Cycle ............................................................................................................ 580 Using Action Injection ................................................................................................... 581 Using the Property Injection Attributes ......................................................................... 582 Manually Requesting an Implementation Object .......................................................... 582 Summary ...................................................................................................................... 583 ■Chapter 19: Filters ��������������������������������������������������������������������������������������������� 585 Preparing the Example Project ..................................................................................... 586 Enabling SSL ....................................................................................................................................... 587 Creating the Controller and View ........................................................................................................ 587 Using Filters ................................................................................................................. 589
📄 Page 19
■ Contents xix Understanding Filters ................................................................................................... 592 Getting Context Data ........................................................................................................................... 593 Using Authorization Filters ........................................................................................... 594 Creating an Authorization Filter .......................................................................................................... 594 Using Action Filters ...................................................................................................... 597 Creating an Action Filter ..................................................................................................................... 598 Creating an Asynchronous Action Filter .............................................................................................. 600 Using Result Filters ...................................................................................................... 601 Creating a Result Filter ....................................................................................................................... 602 Creating an Asynchronous Result Filter .............................................................................................. 604 Creating a Hybrid Action/Result Filter ................................................................................................. 605 Using Exception Filters ................................................................................................. 607 Creating an Exception Filter ............................................................................................................... 608 Using Dependency Injection for Filters ......................................................................... 610 Resolving Filter Dependencies ........................................................................................................... 610 Managing Filter Life Cycles ................................................................................................................ 615 Creating Global Filters .................................................................................................. 618 Understanding and Changing Filter Order .................................................................... 620 Changing Filter Order ......................................................................................................................... 622 Summary ...................................................................................................................... 623 ■Chapter 20: API Controllers ������������������������������������������������������������������������������� 625 Preparing the Example Project ..................................................................................... 626 Creating the Model and Repository .................................................................................................... 626 Creating the Controller and Views ...................................................................................................... 628 Understanding the Role of RESTful Controllers ............................................................ 632 Understanding the Speed Problem ..................................................................................................... 632 Understanding the Efficiency Problem ............................................................................................... 632 Understanding the Openness Problem ............................................................................................... 633
📄 Page 20
■ Contents xx Introducing REST and API Controllers .......................................................................... 633 Creating an API Controller ................................................................................................................... 634 Testing an API Controller ..................................................................................................................... 639 Using the API Controller in the Browser .............................................................................................. 643 Understanding Content Formatting .............................................................................. 645 Understanding the Default Content Policy .......................................................................................... 646 Understanding Content Negotiation .................................................................................................... 647 Specifying an Action Data Format ...................................................................................................... 650 Getting the Data Format from the Route or Query String ................................................................... 651 Enabling Full Content Negotiation ...................................................................................................... 653 Receiving Different Data Formats ....................................................................................................... 654 Summary ...................................................................................................................... 655 ■Chapter 21: Views ��������������������������������������������������������������������������������������������� 657 Preparing the Example Project ..................................................................................... 658 Creating a Custom View Engine ................................................................................... 659 Creating a Custom IView .................................................................................................................... 661 Creating an IViewEngine Implementation ........................................................................................... 662 Registering a Custom View Engine ..................................................................................................... 663 Testing the View Engine ...................................................................................................................... 664 Working with the Razor Engine .................................................................................... 666 Preparing the Example Project ........................................................................................................... 667 Demystifying Razor Views .................................................................................................................. 669 Adding Dynamic Content to a Razor View .................................................................... 673 Using Layout Sections ........................................................................................................................ 673 Using Partial Views ............................................................................................................................. 679 Adding JSON Content to Views ........................................................................................................... 682 Configuring Razor ......................................................................................................... 684 Understanding View Location Expanders ........................................................................................... 685 Summary ...................................................................................................................... 690
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List