ASP.NET Core 1.1 MVC for Beginners
Total Page:16
File Type:pdf, Size:1020Kb
ASP.NET Core 1.1 MVC For Beginners How to build a Video Course Website ASP.NET Core 1.1 MVC For Beginners - How to build a Video Course Website Overview .............................................................................................................................. 1 Setup ................................................................................................................................ 2 Other Titles by the Author ............................................................................................... 2 MVC 5 – How to Build a Membership Website (video course) ................................... 2 Store Secret Data in .NET Core Web App with Azure Key Vault (video course) .......... 3 C# for Beginners: The Tactical Guidebook................................................................... 3 ASP.NET MVC 5 – Building a Website: The Tactical Guidebook .................................. 4 Source Code and Bonus Materials................................................................................... 5 Disclaimer – Who Is This Book for? ................................................................................. 5 Rights ............................................................................................................................... 5 About the Author ............................................................................................................ 6 Part 1: ASP.NET Core 1.1 MVC Your First Application ........................................................ 7 1. Your First ASP.NET Core Application ............................................................................... 8 Creating the Solution and Project ................................................................................... 8 The Project Layout and the File System .................................................................... 11 Important Files .......................................................................................................... 12 Compiling the Solution .............................................................................................. 13 The Startup.cs File ..................................................................................................... 14 Adding a Configuration Service ..................................................................................... 15 Creating a Service .......................................................................................................... 18 Example ..................................................................................................................... 18 Adding the Interface .............................................................................................. 19 Adding the HardcodedMessageService Class ........................................................ 20 Configure and Use the HardcodedMessageService Class ..................................... 20 Add and Use the ConfigurationMessageService Class .......................................... 22 Summary ........................................................................................................................ 24 ASP.NET Core 1.1 MVC For Beginners - How to build a Video Course Website 2. Middleware ................................................................................................................... 25 How Does Middleware Work? ...................................................................................... 25 IApplicationBuilder ........................................................................................................ 26 Handling Exceptions .................................................................................................. 28 Serving Up Static Files ................................................................................................... 31 Serving Up Default Files ................................................................................................. 33 Setting Up ASP.NET MVC ............................................................................................... 33 Adding the MVC NuGet Package ............................................................................... 34 Summary ........................................................................................................................ 36 3. MVC Controllers ............................................................................................................ 37 Routing .......................................................................................................................... 38 Convention-Based Routing ........................................................................................ 39 Implement Routing ................................................................................................ 40 Adding Another Controller .................................................................................... 42 Attribute Routing ....................................................................................................... 43 IActionResult ................................................................................................................. 46 Implementing ContentResult .................................................................................... 47 Using a Model Class ................................................................................................... 47 Introduction to Views .................................................................................................... 49 A View with a Data Collection ....................................................................................... 50 Adding a Data Service .................................................................................................... 53 Summary ........................................................................................................................ 57 4. Models ........................................................................................................................... 58 View Model Example ..................................................................................................... 59 Changing the Folder Structure .................................................................................. 59 Adding the View Model ............................................................................................. 59 Using the View Model ............................................................................................... 60 ASP.NET Core 1.1 MVC For Beginners - How to build a Video Course Website Adding a Details View ................................................................................................ 61 Adding a Create View .................................................................................................... 65 Refactoring the Application ....................................................................................... 66 Adding the HTTP GET Create Action and the Create View ........................................ 68 Adding the VideoEditViewModel Class ..................................................................... 70 Adding the HTTP POST Create Action ........................................................................ 71 Data Annotations ........................................................................................................... 76 Preparing the Crate View for Validation ................................................................... 77 Adding Validation to the Create View ................................................................... 77 Validating the Model on the Server .......................................................................... 78 Adding Data Annotations in the Video Entity and the VideoEditViewModel Class .. 79 Summary ........................................................................................................................ 82 5. Entity Framework .......................................................................................................... 83 Installing Entity Framework and User Secrets ............................................................... 83 Adding the VideoDbContext Class ............................................................................. 85 Configuration in the Startup Class ............................................................................. 86 Adding the Initial Migration and Creating the Database ........................................... 88 Adding the SqlVideoData Service Component .............................................................. 90 Implementing the SqlVideoData Service Component Class ...................................... 90 Summary ........................................................................................................................ 93 6. Razor Views ................................................................................................................... 94 Layout Views .................................................................................................................. 94 Adding the _Layout View ........................................................................................... 95 Altering the Content Views ....................................................................................... 95 The _ViewStart file ........................................................................................................ 99 The _ViewImports file