طراحی پورتال های سازمانی شرکت پروجان

شیرپوینت و پراجکت سرور پروجان

استقرار شیرپوینت و پراجکت سرور

مسیر سایت

کتاب ASP.NET Core 2.0 MVC And Razor Pages For Beginners.pdf

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

دانلود رایگان کتاب ASP.NET Core 2.0 MVC And Razor Pages For Beginners.pdf   

How to Build a Video Course Website

Jonas Fagerberg

Copyright © 2017 by Jonas Fagerberg.

لینک دانلود کتاب ASP.NET Core 2.0 MVC And Razor Pages For Beginners.pdf

 

 

 

Contents

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

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

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

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

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

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

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

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

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

Introduction ...................................................................................................................... 305
Technologies Used in This Chapter....................................................................................... 305
Overview ........................................................................................................................... 305
Adding the Video View ........................................................................................................ 307
Adding the Back to Course Button ........................................................................................ 309
Adding Row and Columns for the Video View Content ............................................................ 310
Adding the _VideoPlayerPartial Partial View ........................................................................... 311
Styling the _VideoPlayerPartial Partial View ........................................................................... 315
Add JWPlayer ...................................................................................................................... 315
Create a Video Player ........................................................................................................... 317
Add the Video Player to the Video View ................................................................................. 317
Adding Properties to the LessonInfoDTO Class ...................................................................... 318
Adding the _VideoComingUpPartial Partial View ..................................................................... 319
Styling the _VideoComingUpPartial Partial View ..................................................................... 324
Adding the _InstructorBioPartial Partial View ......................................................................... 324
Summary ............................................................................................................................ 326
21. Creating the Database Tables ......................................................................................... 327
Introduction ........................................................................................................................ 327
Technologies Used in This Chapter......................................................................................... 327
Overview ............................................................................................................................. 327
Adding the Tables ................................................................................................................ 328
Adding the Entity Classes to the VODContext ......................................................................... 328
Creating the Tables .............................................................................................................. 329
Adding Seed Data ................................................................................................................. 330
Summary ............................................................................................................................. 338
22. The Database Read Service ............................................................................................. 339
Introduction ......................................................................................................................... 339

Technologies Used in This Chapter......................................................................................... 339
Overview ............................................................................................................................. 339
Adding the DbReadService Service ........................................................................................ 339
Adding the Service Interface and Class .................................................................................. 340
Fetching All Records in a Table (Get) ..................................................................................... 341
Finding an Entity’s Intrinsic Entity Properties (GetEntityNames) ............................................... 342
Fetching a Record by Id from a Table (Get) ........................................................................... 345
Fetching a Record in a Table with a Composite Primary Key (Get) ............................................ 347
Fetch All Records and Related Records for an Entity (GetWithIncludes)..................................... 349
Converting an Entity List to a List of SelectList Items (GetSelectList) ........................................ 351
Summary ............................................................................................................................. 353
23. SQL Data Repository ....................................................................................................... 355
Introduction ......................................................................................................................... 355
Technologies Used in This Chapter......................................................................................... 355
Overview ............................................................................................................................. 355
Adding the SqlReadRepository Class ...................................................................................... 355
Implementing the GetCourses Method.....................................................................................357
Implementing the GetCourse Method .................................................................................... 358
Implementing the GetVideo Method ...................................................................................... 359
Implementing the GetVideos Method ..................................................................................... 360
Summary ............................................................................................................................. 361
Part 3: Razor Pages How to Build the Administrator Website ................................................... 363
24. Adding the Admin Project ................................................................................................ 365
Overview ............................................................................................................................. 365
Technologies Used in This Chapter......................................................................................... 366
Creating the Admin Solution .................................................................................................. 366
Summary ............................................................................................................................. 371

25. The Administrator Dashboard .......................................................................................... 373
Introduction ......................................................................................................................... 373
Technologies Used in This Chapter.......................................................................................... 373
Modifying the Navigation Menu .............................................................................................. 374
Creating the Dashboard.......................................................................................................... 375
Adding the Count Method to the DbReadService ...................................................................... 376
Adding the CardViewModel Class ............................................................................................ 377
Adding the _CardPartial Partial View ....................................................................................... 378
Calling the Count Method from the Index Razor Page .............................................................. 379
Styling the _CardPartial View ................................................................................................. 382
Modifying the Index Razor Page ............................................................................................. 385
Summary .............................................................................................................................. 387
26. The Admin Menu ............................................................................................................. 389
Introduction ......................................................................................................................... 389
Technologies Used in This Chapter.......................................................................................... 389
Overview .............................................................................................................................. 389
Adding the _AdminMenuPartial Partial View.............................................................................. 389
Summary .............................................................................................................................. 393
27. Custom Button Tag Helper ................................................................................................ 395
Introduction ........................................................................................................................... 395
Technologies Used in This Chapter............................................................................................ 395
Overview ................................................................................................................................ 396
Implementing the Page-Button Tag Helper................................................................................. 397
Creating the Tag Helper ........................................................................................................... 397
URL Parameter Values .............................................................................................................. 401
Glyphicons ............................................................................................................................... 403
Turning Links into Buttons ........................................................................................................ 405

Styling the Buttons ................................................................................................................... 406
Summary ................................................................................................................................ 407
28. The Database Write Service ................................................................................................ 409
Introduction ............................................................................................................................ 409
Technologies Used in This Chapter............................................................................................. 409
Overview ................................................................................................................................. 409
Adding the DbWriteService Service ............................................................................................ 409
Adding the Service Interface and Class ...................................................................................... 409
The Add Method ....................................................................................................................... 411
The Delete Method.................................................................................................................... 412
The Update Method .................................................................................................................. 414
The Update Method for Entities with a Combined Primary Key ..................................................... 415
Summary ................................................................................................................................. 417
29. The User Service ................................................................................................................ 419
Introduction ............................................................................................................................ 419
Technologies Used in This Chapter............................................................................................. 419
Overview ................................................................................................................................. 419
Adding the UserService Service ................................................................................................. 419
The UserPageModel Class ......................................................................................................... 419
Adding the UserPageModel Class ............................................................................................... 420
Adding the Service Interface and Class ...................................................................................... 420
The GetUsers Method ............................................................................................................... 422
The GetUser Method ................................................................................................................ 423
The RegisterUserPageModel Class ............................................................................................. 425
The AddUser Method ................................................................................................................ 426
The UpdateUser Method ........................................................................................................... 427
The DeleteUser Method ............................................................................................................ 431

Summary ................................................................................................................................ 433
30. The User Razor Pages ........................................................................................................ 435
Technologies Used in This Chapter............................................................................................ 435
Overview ................................................................................................................................ 435
The [TempData] Attribute ........................................................................................................ 436
The Users/Index Razor Page .................................................................................................... 436
Altering the IndexModel Class .................................................................................................. 437
Altering the Index Razor Page .................................................................................................. 439
The Users/Create Razor Page ................................................................................................... 444
Altering the CreateModel Class ................................................................................................. 444
Altering the Create Razor Page ................................................................................................. 448
The Users/Edit Razor Page ....................................................................................................... 454
Altering the EditModel class ...................................................................................................... 454
Altering the Edit Razor Page ..................................................................................................... 456
The Users/Delete Razor Page .................................................................................................... 458
Altering the DeleteModel Class .................................................................................................. 458
Altering the Delete Razor Page................................................................................................... 459
Summary ................................................................................................................................ 462
31. The StatusMessage Tag Helper ........................................................................................... 463
Introduction ............................................................................................................................ 463
Technologies Used in This Chapter............................................................................................ 464
Adding the Tag Helper Class .................................................................................................... 464
Summary ................................................................................................................................ 466
32. The Remaining Razor Pages ................................................................................................ 467
Overview ................................................................................................................................. 467
Technologies Used in This Chapter............................................................................................... 467
The Video Razor Pages ............................................................................................................... 467

The IndexModel Class ................................................................................................................. 471
The Index Razor Page ................................................................................................................. 472
The CreateModel Class ................................................................................................................ 474
The Create Razor Page ................................................................................................................ 476
The EditModel Class ..................................................................................................................... 479
The Edit Razor Page ..................................................................................................................... 480
The DeleteModel Class .................................................................................................................. 483
The Delete Razor Page .................................................................................................................. 484
The Downloads Razor Pages ......................................................................................................... 487
The IndexModel Class ................................................................................................................... 487
The Index Razor Page ................................................................................................................... 487
The CreateModel Class .................................................................................................................. 487
The Create Razor Page .................................................................................................................. 488
The EditModel Class ...................................................................................................................... 488
The Edit Razor Page ...................................................................................................................... 489
The DeleteModel Class .................................................................................................................. 489
The Delete Razor Page .................................................................................................................. 489
The Instructors Razor Pages .......................................................................................................... 490
The IndexModel Class ................................................................................................................... 490
The Index Razor Page ................................................................................................................... 490
The CreateModel Class ................................................................................................................... 491
The Create Razor Page ................................................................................................................... 491
The EditModel Class ....................................................................................................................... 491
The Edit Razor Page ....................................................................................................................... 492
The DeleteModel Class ................................................................................................................... 492
The Delete Razor Page ................................................................................................................... 493
The Courses Razor Pages ............................................................................................................... 493

The IndexModel Class .................................................................................................................... 493
The Index Razor Page .................................................................................................................... 493
The CreateModel Class ................................................................................................................... 494
The Create Razor Page ................................................................................................................... 494
The EditModel Class ....................................................................................................................... 495
The Edit Razor Page ....................................................................................................................... 495
The DeleteModel Class .................................................................................................................... 496
The Delete Razor Page ................................................................................................................... 496
The Modules Razor Pages ................................................................................................................ 496
The IndexModel Class ..................................................................................................................... 496
The Index Razor Page ..................................................................................................................... 497
The CreateModel Class .................................................................................................................... 497
The Create Razor Page .................................................................................................................... 498
The EditModel Class ........................................................................................................................ 498
The Edit Razor Page ........................................................................................................................ 499
The DeleteModel Class .................................................................................................................... 499
The Delete Razor Page .................................................................................................................... 499
The UserCourses Razor Pages ......................................................................................................... 499
The IndexModel Class ..................................................................................................................... 500
The Index Razor Page ..................................................................................................................... 500
The CreateModel Class .................................................................................................................... 501
The Create Razor Page .................................................................................................................... 502
The UserCoursePageModel Class ...................................................................................................... 502
The EditModel Class ........................................................................................................................ 503
The Edit Razor Page ........................................................................................................................ 506
The DeleteModel Class .................................................................................................................... 507
The Delete Razor Page .................................................................................................................... 508

Summary ....................................................................................................................................... 508
Other Books by the Author .............................................................................................................. 509
Video Courses by the Author ............................................................................................................ 510
MVC 5 – How to Build a Membership Website (video course) .............................................................. 510
Store Secret Data in a .NET Core Web App with Azure Key Vault (video course) ................................... 510

 

Overview
I would like to welcome you to ASP.NET Core 2.0 MVC & Razor Pages for Beginners. This book will guide you through creating your very first MVC and Razor Page applications. To get the most from this book, you should have a basic understanding of HTML and be familiar with the C# language.
ASP.NET Core is a new framework from Microsoft. It has been designed from the ground up to be fast and flexible and to work across multiple platforms. ASP.NET Core is the framework to use for your future ASP.NET applications.
The first application you build will evolve into a basic MVC application, starting with an empty template. You will add the necessary pieces one at a time to get a good under-standing of how things fit together. The focus is on installing and configuring middleware, services, and other frameworks. Styling with CSS is not a priority in this application; you’ll learn more about that in the second application you build.
You will install middleware to create a processing pipeline, and then look at the MVC framework. If you already are familiar with MVC or Web API from previous versions of ASP.NET, you will notice some similarities.
There still are model classes, which are used as data carriers between the controller and its views. There are, however, many new features that you will learn, such as Tag Helpers and view components. You will also work with Entity Framework to store and retrieve data, implement authentication with ASP.NET Identity framework, install CSS libraries such as Bootstrap, and install JavaScript libraries such as JQuery. Note that dependency injection now is a first-class design pattern.
The second solution you will create will contain three projects: one for the database and services (referenced from the other two projects), one MVC project for the user UI, and one Razor Page project for the administrator UI. Pre-existing MVC and Razor Page templates will be used for the two UI projects, and an empty template will be used for the database project.
You will modify the database support installed by the templates to instead target the database created by the database project. Only minor modifications will be made to the authentication and routing provided by the templates.

By the end of this book you will be able to create simple ASP.NET Core 2.0 applications on your own, which can create, edit, delete, and view data in a database.
All applications you will build revolve around video data and playing videos. In one application, you will be able to add and edit video titles, and in another, you will build a more sophisticated customer portal, where users can view the course videos that they have access to.

 

 

لینک دانلود کتاب ASP.NET Core 2.0 MVC And Razor Pages For Beginners.pdf

 

 

 

عضویت در خبرنامه