ebook img

ASP.NET Core 2.0 MVC And Razor Pages For Beginners PDF

528 Pages·2018·10.96 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview ASP.NET Core 2.0 MVC And Razor Pages For Beginners

ASP.NET Core 2.0 MVC & Razor Pages for Beginners How to Build a Video Course Website ASP.NET Core 2.0 MVC For Beginners - How to build a Video Course Website Overview .............................................................................................................................. 1 Setup ................................................................................................................................ 2 Book Version .................................................................................................................... 2 Other Books by the Author.............................................................................................. 3 Video Courses Produced by the Author .......................................................................... 4 MVC 5 – How to Build a Membership Website (video course) ................................... 4 Store Secret Data in .NET Core Web App with Azure Key Vault (video course) .......... 4 Source Code ..................................................................................................................... 4 Disclaimer – Who Is This Book for? ................................................................................. 5 Rights ............................................................................................................................... 5 About the Author ............................................................................................................ 5 Part 1: ASP.NET Core 2.0 MVC Your First Application ......................................................... 7 1. Your First ASP.NET Core Application ............................................................................... 9 The Project Layout and the File System .................................................................... 11 Important Files .......................................................................................................... 12 Compiling the Solution .............................................................................................. 14 The Startup.cs File ..................................................................................................... 15 Adding a Configuration Service ..................................................................................... 16 Creating a Service .......................................................................................................... 18 Example ..................................................................................................................... 19 Adding the IMessageService Interface .................................................................. 20 Adding the HardcodedMessageService Class ........................................................ 20 Configure and Use the HardcodedMessageService Class ..................................... 21 Add and Use the ConfigurationMessageService Class .......................................... 23 Summary ........................................................................................................................ 25 2. Middleware ................................................................................................................... 27 ASP.NET Core 2.0 MVC & Razor Pages for Beginners How Does Middleware Work? ...................................................................................... 27 IApplicationBuilder ........................................................................................................ 28 Handling Exceptions .................................................................................................. 30 Serving Up Static Files ................................................................................................... 33 Setting Up ASP.NET MVC ............................................................................................... 34 Summary ........................................................................................................................ 37 3. MVC Controllers ............................................................................................................ 39 Routing .......................................................................................................................... 40 Convention-Based Routing ........................................................................................ 41 Implement Routing ................................................................................................ 41 Adding Another Controller .................................................................................... 42 Attribute Routing ....................................................................................................... 43 IActionResult ................................................................................................................. 47 Implementing ContentResult .................................................................................... 47 Using a Model Class and ObjectResult ...................................................................... 48 Introduction to Views .................................................................................................... 50 A View with a Data Collection ....................................................................................... 52 Adding a Data Service .................................................................................................... 54 Summary ........................................................................................................................ 59 4. Models ........................................................................................................................... 61 View Model Example ..................................................................................................... 62 Changing the Folder Structure .................................................................................. 62 Adding the View Model ............................................................................................. 62 Using the View Model ............................................................................................... 63 Adding a Details View .................................................................................................... 64 Adding a Create View .................................................................................................... 69 Refactoring the Application ....................................................................................... 70 ASP.NET Core 2.0 MVC & Razor Pages for Beginners Adding the HTTP GET Create Action and the Create View ........................................ 72 Adding the VideoEditViewModel Class ..................................................................... 75 Adding the HTTP POST Create Action ........................................................................ 76 Data Annotations ........................................................................................................... 81 Preparing the Create View for Validation ................................................................. 82 Adding Validation to the Create View ................................................................... 82 Validating the Model on the Server .......................................................................... 83 Adding Data Annotations in the Video Entity and VideoEditViewModel Class ......... 84 Summary ........................................................................................................................ 87 5. Entity Framework .......................................................................................................... 89 Adding the VideoDbContext Class ............................................................................. 89 Configuration in the Startup Class ............................................................................. 91 Adding the Initial Migration and Creating the Database ........................................... 93 Adding the SqlVideoData Service Component .............................................................. 95 Implementing the SqlVideoData Service Component Class ...................................... 95 Summary ........................................................................................................................ 99 6. Razor Views ................................................................................................................. 101 Layout Views ................................................................................................................ 101 Adding the _Layout View ......................................................................................... 102 Altering the Content Views ..................................................................................... 102 The _ViewStart file ...................................................................................................... 106 The _ViewImports file ................................................................................................. 107 Tag Helpers .................................................................................................................. 108 Altering the Index View ........................................................................................... 109 Adding an Edit View and Its Actions ........................................................................ 111 Refactoring the IVideoData Service ......................................................................... 113 Partial Views ................................................................................................................ 117 ASP.NET Core 2.0 MVC & Razor Pages for Beginners View Components ....................................................................................................... 120 Adding a View Component for the IMessageService Service .................................. 120 Summary ...................................................................................................................... 123 7. Forms Authentication .................................................................................................. 125 Adding the Authorize and AlowAnonymous Attributes .............................................. 126 Configuring the Identity Framework ........................................................................... 127 Creating the AspNet Identity Database Tables ........................................................... 129 User Registration ......................................................................................................... 130 Login and Logout ......................................................................................................... 137 Adding the _Login Partial View................................................................................ 137 Adding the Logout Action ........................................................................................ 140 Adding the LoginViewModel Class .......................................................................... 141 Adding the HTTP GET Login Action .......................................................................... 142 Adding the HTTP POST Login Action ........................................................................ 142 Adding the Login View ............................................................................................. 144 Summary ...................................................................................................................... 147 8. Front-End Frameworks ................................................................................................ 149 Installing Bower and the Frameworks ......................................................................... 149 Styling with Bootstrap ................................................................................................. 152 Adding a Navigation Bar .......................................................................................... 153 Styling the Index View ............................................................................................. 155 Adding Client-Side Validation ...................................................................................... 157 Summary ...................................................................................................................... 159 Part 2: MVC How to Build a Video Course Website ....................................................... 161 9. The Use Case................................................................................................................ 163 Introduction ................................................................................................................. 163 The Use Case ............................................................................................................... 163 ASP.NET Core 2.0 MVC & Razor Pages for Beginners The User Interface (MVC) ........................................................................................ 164 Login and Register User ........................................................................................... 164 The Administrator Interface (Razor Pages) ............................................................. 165 Conclusion ................................................................................................................... 165 Login and Register ................................................................................................... 165 The User Dashboard View ....................................................................................... 166 The Course View ...................................................................................................... 167 The Video View ........................................................................................................ 168 The Administrator Dashboard Razor Page .............................................................. 169 A Typical Administrator Index Razor Page............................................................... 170 A Typical Administrator Create Razor Page ............................................................. 171 A Typical Administrator Edit Razor Page ................................................................. 172 A Typical Administrator Delete Razor Page ............................................................. 173 10. Setting Up the Solution ............................................................................................. 175 Introduction ................................................................................................................. 175 Technologies Used in This Chapter.......................................................................... 175 Overview ...................................................................................................................... 175 Creating the Solution ................................................................................................... 175 Installing AutoMapper ................................................................................................. 178 Creating the Database ................................................................................................. 179 Adding the Database Project ................................................................................... 180 Adding the User Class .............................................................................................. 180 Adding the Database Context .................................................................................. 180 Summary ...................................................................................................................... 183 11. Login .......................................................................................................................... 185 Introduction ................................................................................................................. 185 Technologies Used in This Chapter.......................................................................... 185 ASP.NET Core 2.0 MVC & Razor Pages for Beginners Redirecting to the Login View ..................................................................................... 185 Styling the Login View ................................................................................................. 187 Adding the login.css Stylesheet ............................................................................... 188 Changing the Layout of the Login View ................................................................... 189 Styling the Login View ............................................................................................. 193 Summary ...................................................................................................................... 195 12. Register User ............................................................................................................. 197 Introduction ................................................................................................................. 197 Technologies Used in This Chapter.......................................................................... 197 Overview ...................................................................................................................... 197 Changing the Layout of the Register View .................................................................. 198 Styling the Register View ......................................................................................... 200 Changing the Register Action .................................................................................. 202 Testing the Registration Form ................................................................................. 202 Summary ...................................................................................................................... 204 13. Modifying the Navigation Bar .................................................................................... 205 Introduction ................................................................................................................. 205 Technologies Used in This Chapter.......................................................................... 205 Overview ...................................................................................................................... 205 Styling the Navigation Bar ........................................................................................... 206 Remove the Register and Login Links .......................................................................... 208 Add the Drop-Down Menu .......................................................................................... 208 Style the Drop-Down Menu ..................................................................................... 210 Summary ...................................................................................................................... 211 14. Data Transfer Objects ................................................................................................ 213 Introduction ................................................................................................................. 213 Technologies Used in This Chapter.......................................................................... 213 ASP.NET Core 2.0 MVC & Razor Pages for Beginners Overview ...................................................................................................................... 213 The DTOs ..................................................................................................................... 213 Adding the DTOs ...................................................................................................... 218 The View Models ......................................................................................................... 221 Adding the View Models ......................................................................................... 222 Summary ...................................................................................................................... 223 15. Entity Classes ............................................................................................................. 225 Introduction ................................................................................................................. 225 Technologies Used in This Chapter.......................................................................... 225 Overview ...................................................................................................................... 225 The Entities .................................................................................................................. 225 The Video Entity ...................................................................................................... 225 The Download Entity ............................................................................................... 226 The Instructor Entity ................................................................................................ 227 The Course Entity .................................................................................................... 228 The Module Entity ................................................................................................... 228 The UserCourse Entity ............................................................................................. 229 Adding the Entity Classes ............................................................................................ 230 Summary ...................................................................................................................... 233 16. Mock Data Repository ............................................................................................... 235 Introduction ................................................................................................................. 235 Technologies Used in This Chapter.......................................................................... 235 Overview ...................................................................................................................... 235 Add the IReadRepository Interface and MockReadRepository Class .......................... 235 Add Data to the MockReadRepository Class ............................................................... 236 The Course List ........................................................................................................ 236 The UserCourses List ............................................................................................... 237 ASP.NET Core 2.0 MVC & Razor Pages for Beginners The Modules List...................................................................................................... 237 The Downloads List .................................................................................................. 237 The Instructors List .................................................................................................. 238 The Videos List ......................................................................................................... 238 The GetCourses Method ............................................................................................. 239 Testing the GetCourses Method.............................................................................. 240 The GetCourse Method ............................................................................................... 241 Testing the GetCourse Method ............................................................................... 243 The GetVideo Method ................................................................................................. 243 Testing the GetVideo Method ................................................................................. 244 The GetVideos Method ............................................................................................... 245 Testing the GetVideos Method ............................................................................... 247 Summary ...................................................................................................................... 248 17. The Membership Controller and AutoMapper.......................................................... 249 Introduction ................................................................................................................. 249 Technologies Used in This Chapter.......................................................................... 249 Overview ...................................................................................................................... 249 Adding the Membership Controller ............................................................................ 250 Adding the Controller .............................................................................................. 250 Configuring AutoMapper ............................................................................................. 253 Implementing the Action Methods ............................................................................. 255 The Dashboard Action Method ............................................................................... 255 The Course Action Method ...................................................................................... 259 The Video Action Method ....................................................................................... 262 Summary ...................................................................................................................... 268 18. The Dashboard View .................................................................................................. 269 Introduction ................................................................................................................. 269 ASP.NET Core 2.0 MVC & Razor Pages for Beginners Technologies Used in This Chapter.......................................................................... 269 Overview ...................................................................................................................... 269 Implementing the Dashboard View ............................................................................. 270 Adding the Dashboard View .................................................................................... 270 Iterating Over the Courses in the Dashboard View ................................................. 272 Creating the _CoursePanelPartial Partial View ....................................................... 274 Styling the Dashboard View and the _CoursePanelPartial Partial View ................. 277 Summary ...................................................................................................................... 280 19. The Course View ........................................................................................................ 281 Introduction ................................................................................................................. 281 Technologies Used in This Chapter.......................................................................... 281 Overview ...................................................................................................................... 281 Adding the Course View .............................................................................................. 282 Adding the Back to Dashboard Button .................................................................... 284 Adding the Course.css Style Sheet .......................................................................... 285 Adding the Course Information to the View ........................................................... 286 Styling the Course Information Section ................................................................... 288 Adding Columns for the Modules and the Instructor Bio ........................................... 289 Adding the Modules .................................................................................................... 290 Adding the Videos ....................................................................................................... 291 Styling the _ModuleVideosPartial View .................................................................. 295 Adding the Downloads ................................................................................................ 297 Styling the _ModuleDownloadsPartial View ........................................................... 300 Adding the Instructor Bio ............................................................................................ 301 Styling the _InstructorBioPartial Partial View ......................................................... 303 Summary ...................................................................................................................... 303 20. The Video View .......................................................................................................... 305

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.