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

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

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

مسیر سایت

کتاب ASP.NET Core 1.1 MVC for Beginners.pdf

ASP.NET Core 1.1 MVC for Beginners.pdf How to build a Video Course Website

دانلود رایگان کتاب ASP.NET Core 1.1 MVC for Beginners.pdf  

How to build a Video Course Website

Joanas Fagerberg

Copyright © 2017 by Jonas Fagerberg

لینک دانلود کتاب ASP.NET Core 1.1 MVC for Beginners.pdf

 

 

Table of Contents

 

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

 

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

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 ................................................................................................. 100
Tag Helpers .................................................................................................................. 101

Altering the Index View ........................................................................................... 102
Adding an Edit View and Its Actions ........................................................................ 103
Refactoring the IVideoData Service ......................................................................... 106
Partial Views ................................................................................................................ 109
View Components ....................................................................................................... 112
Adding a View Component for the IMessageService Service .................................. 112
Summary ...................................................................................................................... 115

 

7. Forms Authentication .................................................................................................. 116

Adding the Authorize and AlowAnonymous Attributes .............................................. 117
Configuring the Identity Framework ........................................................................... 118
Creating the AspNet Identity Database Tables ........................................................... 120
User Registration ......................................................................................................... 121
Login and Logout ......................................................................................................... 126
Adding the _Login Partial View................................................................................ 126
Adding the Logout Action ........................................................................................ 128
Adding the LoginViewModel Class .......................................................................... 129
Adding the HTTP GET Login Action .......................................................................... 130
Adding the HTTP POST Login Action ........................................................................ 131
Adding the Login View ............................................................................................. 133
Summary ...................................................................................................................... 135

 

8. Front-End Frameworks ................................................................................................ 137

Installing Bower and the Frameworks ......................................................................... 137
Styling with Bootstrap ................................................................................................. 140
Adding a Navigation Bar .......................................................................................... 141
Styling the Index View ............................................................................................. 143
Adding Client-Side Validation ...................................................................................... 145
Summary ...................................................................................................................... 147

 

Part 2: ASP.NET Core 1.1 MVC How to Build a Video Course Website ........................... 148

9. The Use Case................................................................................................................ 149

Introduction ................................................................................................................. 149
The Use Case ............................................................................................................... 149
The User Interface ................................................................................................... 149
Login and Register User ........................................................................................... 150
The Administrator Views ......................................................................................... 150
Conclusion ................................................................................................................... 151
Login and Register ................................................................................................... 151
The Dashboard View ................................................................................................ 151
The Course View ...................................................................................................... 152
The Video View ........................................................................................................ 154
A Typical Administrator Index View ........................................................................ 155
A Typical Administrator Create View ....................................................................... 155
A Typical Administrator Edit View ........................................................................... 156
A Typical Administrator Delete View ....................................................................... 157
A Typical Administrator Details View ...................................................................... 158

 

10. Setting Up the Solution ............................................................................................. 159

Introduction ................................................................................................................. 159
Technologies Used in This Chapter.......................................................................... 159
Overview ...................................................................................................................... 159
Creating the Solution ................................................................................................... 159
Installing AutoMapper ................................................................................................. 161
Summary ...................................................................................................................... 162

 

11. Login .......................................................................................................................... 163

Introduction ................................................................................................................. 163
Technologies Used in This Chapter.......................................................................... 163

Creating the Database ................................................................................................. 163
Redirecting to the Login View ..................................................................................... 165
Styling the Login View ................................................................................................. 167
Adding the login.css Stylesheet ............................................................................... 168
Changing the Layout of the View ............................................................................ 169
Styling the Login View ............................................................................................. 173
Summary ...................................................................................................................... 175

 

12. Register User ............................................................................................................. 176

Introduction ................................................................................................................. 176
Technologies Used in This Chapter.......................................................................... 176
Overview ...................................................................................................................... 176
Changing the Layout of the View ............................................................................ 177
Styling the Register View ......................................................................................... 179
Testing the Registration Form ................................................................................. 181
Summary ...................................................................................................................... 183

 

13. Modifying the Navigation Bar .................................................................................... 184

Introduction ................................................................................................................. 184
Technologies Used in This Chapter.......................................................................... 184
Overview ...................................................................................................................... 184
Styling the Navigation Bar ........................................................................................... 185
Remove the Register and Login links ........................................................................... 186
Add the Drop-Down Menu .......................................................................................... 187
Style the Drop-Down Menu ..................................................................................... 189
Summary ...................................................................................................................... 190

 

14. Data Transfer Objects ................................................................................................ 191

Introduction ................................................................................................................. 191
Technologies Used in This Chapter.......................................................................... 191

Overview ...................................................................................................................... 191
The DTOs ..................................................................................................................... 191
Adding the DTOs ...................................................................................................... 196
The View Models ......................................................................................................... 199
Adding the View Models ......................................................................................... 200
Summary ...................................................................................................................... 201

 

15. Entity Classes ............................................................................................................. 202

Introduction ................................................................................................................. 202
Technologies Used in This Chapter.......................................................................... 202
Overview ...................................................................................................................... 202
The Entities .................................................................................................................. 202
The Video Entity ...................................................................................................... 202
The Download Entity ............................................................................................... 203
The Instructor Entity ................................................................................................ 204
The Course Entity .................................................................................................... 204
The Module Entity ................................................................................................... 205
The UserCourse Entity ............................................................................................. 205
Adding the Entity Classes ............................................................................................ 206
Summary ...................................................................................................................... 209

 

16. Mock Data Repository ............................................................................................... 210

Introduction ................................................................................................................. 210
Technologies Used in This Chapter.......................................................................... 210
Overview ...................................................................................................................... 210
Add the IReadRepository Interface and MockReadRepository Class .......................... 210
Add Data to the MockReadRepository Class ............................................................... 211
The Course List ........................................................................................................ 211
The UserCourses List ............................................................................................... 212

The Modules List...................................................................................................... 212
The Downloads List .................................................................................................. 212
The Instructors List .................................................................................................. 213
The Videos List ......................................................................................................... 213
The GetCourses Method ............................................................................................. 214
Testing the GetCourses Method.............................................................................. 215
The GetCourse Method ............................................................................................... 215
Testing the GetCourse Method ............................................................................... 217
The GetVideo Method ................................................................................................. 218
Testing the GetVideo Method ................................................................................. 218
The GetVideos Method ............................................................................................... 219
Testing the GetVideos Method ............................................................................... 220
Summary ...................................................................................................................... 223

 

17. The Membership Controller and AutoMapper.......................................................... 224

Introduction ................................................................................................................. 224
Technologies Used in This Chapter.......................................................................... 224
Overview ...................................................................................................................... 224
Adding the Membership Controller ............................................................................ 225
Adding the Controller .............................................................................................. 225
Configuring AutoMapper ............................................................................................. 228
Implementing the Action Methods ............................................................................. 230
The Dashboard Action Method ............................................................................... 230
The Course Action Method ...................................................................................... 234
The Video Action Method ....................................................................................... 238
Summary ...................................................................................................................... 243

 

18. The Dashboard View .................................................................................................. 244

Introduction ................................................................................................................. 244

Technologies Used in This Chapter.......................................................................... 244
Overview ...................................................................................................................... 244
Implementing the Dashboard View ............................................................................. 245
Adding the Dashboard View .................................................................................... 245
Iterating Over the Courses in the Dashboard View ................................................. 247
Creating the _CoursePanelPartial Partial View ....................................................... 250
Styling the Dashboard View and the _CoursePanelPartial Partial View ................. 252
Summary ...................................................................................................................... 256

 

19. The Course View ........................................................................................................ 257

Introduction ................................................................................................................. 257
Technologies Used in This Chapter.......................................................................... 257
Overview ...................................................................................................................... 257
Adding the Course View .............................................................................................. 258
Adding the Back to Dashboard Button .................................................................... 260
Adding the Course.css Style Sheet .......................................................................... 262
Adding the Course Information to the View ........................................................... 262
Styling the Course Information Section ................................................................... 265
Adding Columns for the Modules and the Instructor Bio ........................................... 266
Adding the Modules .................................................................................................... 267
Adding the Videos ....................................................................................................... 268
Styling the _ModuleVideosPartial View .................................................................. 272
Adding the Downloads ................................................................................................ 274
Styling the _ModuleDownloadsPartial View ........................................................... 277
Adding the Instructor Bio ............................................................................................ 278
Styling the _InstructorBioPartial Partial View ......................................................... 280
Summary ...................................................................................................................... 280

 

20. The Video View .......................................................................................................... 281

Introduction ................................................................................................................. 281
Technologies Used in This Chapter.......................................................................... 281
Overview ...................................................................................................................... 281
Adding the Video View ................................................................................................ 283
Adding the Back to Course Button .......................................................................... 285
Adding Row and Columns for the Video View Content ........................................... 286
Adding the _VideoPlayerPartial Partial View .......................................................... 287
Styling the _VideoPlayerPartial Partial View ........................................................... 290
Add JWPlayer ............................................................................................................... 291
Create a Video Player .............................................................................................. 292
Add the Video Player to the Video View ................................................................. 292
Adding Properties to the LessonInfoDTO Class ....................................................... 293
Adding the _VideoComingUpPartial Partial View ................................................... 294
Styling the _VideoComingUpPartial Partial View .................................................... 299
Adding the _InstructorBioPartial Partial View ......................................................... 299
Summary ...................................................................................................................... 301

 

21. SQL Data Repository .................................................................................................. 302

Introduction ................................................................................................................. 302
Technologies used in this chapter ........................................................................... 302
Overview ...................................................................................................................... 302
Adding the Tables ........................................................................................................ 303
Adding the Entity Classes to the ApplicationDbContext ......................................... 303
Adding Seed Data .................................................................................................... 305
Creating the Tables .................................................................................................. 310
Adding the SqlReadRepository .................................................................................... 312
Implementing the GetCourses method ................................................................... 312
Implementing the GetCourse Method .................................................................... 314

Implementing the GetVideo Method ...................................................................... 316
Implementing the GetVideos Method .................................................................... 316
Summary ...................................................................................................................... 317

 

22. The Admin Menu ....................................................................................................... 318

Introduction ................................................................................................................. 318
Technologies Used in This Chapter.......................................................................... 318
Overview ...................................................................................................................... 318
Adding the _AdminMenuPartial Partial View.............................................................. 319
Summary ...................................................................................................................... 322

 

23. Controllers and Views ................................................................................................ 323

Introduction ................................................................................................................. 323
Technologies used in this chapter ........................................................................... 323
Overview ...................................................................................................................... 323
Adding Controllers and Views ..................................................................................... 327
Fixing the Drop-Downs in the Views ....................................................................... 330
The Index Action ...................................................................................................... 334
The Index View ........................................................................................................ 335
The Details Action .................................................................................................... 336
The Details View ...................................................................................................... 337
The HTTP GET Delete Action ................................................................................... 338
The HTTP POST Delete Confirmed Action ................................................................ 338
The Delete View....................................................................................................... 338
The HTTP GET Create Action ................................................................................... 339
The HTTP POST Create Action ................................................................................. 339
The Create View ...................................................................................................... 340
The HTTP GET Edit Action ........................................................................................ 343
The HTTP POST Edit Action ...................................................................................... 343

The Edit View ........................................................................................................... 345
Summary ...................................................................................................................... 345

 

24. The UserCourse Controller ........................................................................................ 346

Technologies used in this chapter ........................................................................... 346
Overview ...................................................................................................................... 346
The UserCourse Controller .......................................................................................... 346
Creating the UserCoursesController Class ............................................................... 347
Adding the Index Action and View .......................................................................... 348
Adding the Details Action and View ........................................................................ 352
Adding the HTTP GET Create Action and View ........................................................ 356
Adding the HTTP POST Create Action ...................................................................... 359
Adding the HTTP GET Edit Action and View ............................................................ 361
Adding the HTTP POST Edit Action .......................................................................... 364
Adding the HTTP GET Delete Action and View ........................................................ 368
Adding the HTTP POST Delete Confirmed Action ..................................................... 370
Authorize and Route Attributes in the Admin Controllers .......................................... 372
Changing the Route and Adding Authorization ....................................................... 372
Summary ...................................................................................................................... 373

 

25. Custom Tag Helper .................................................................................................... 374

Introduction ................................................................................................................. 374
Technologies Used in This Chapter.......................................................................... 374
Overview ...................................................................................................................... 375
Implementing the Button-Container Tag Helper ........................................................ 375
Creating the Tag Helper ........................................................................................... 376
Multiple Actions and Descriptions .......................................................................... 379
URL Parameter Values ............................................................................................. 381
Glyphicons ............................................................................................................... 384

Turning Links into Buttons ....................................................................................... 386
Styling the Views ..................................................................................................... 386
Replacing Links with Buttons ................................................................................... 389
An Alternate Button-Container Tag Helper ................................................................. 391
Summary ...................................................................................................................... 394
Other Titles by the Author ............................................................................................... 395
Video Courses .............................................................................................................. 395
MVC 5 – How to Build a Membership Website (video course) ............................... 395
Store Secret Data in a .NET Core Web App with Azure Key Vault (video course) ... 395
Books ........................................................................................................................... 396
ASP.NET MVC 5 – Building a Website: The Tactical Guidebook .............................. 396
C# for Beginners: The Tactical Guidebook............................................................... 397

 

 

Overview

I would like to welcome you to ASP.NET Core 1.1 MVC for Beginners. This book will guide you through creating your very first MVC 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 application you will create, starts from a pre-existing MVC template, where the main pipeline already has been added. Things like database support, authentication, and routing have already been implemented, ready to use.
By the end of this book you will be able to create simple ASP.NET Core MVC applications on your own, which can create, edit, delete, and view data in a database.
Both applications you will build revolve around video data and playing videos. In the first application, you will be able to add and edit video titles, and in the second you will build a more sophisticated customer portal, where users can view the course videos that they have access to.

 

Setup

In this book, you will be using C# and any Visual Studio 2017 version that you have access to. You can even use the free Visual Studio Community 2017 version, which you can down-load from visualstudio.com/downloads. You can develop ASP.NET Core applications on Mac OS X and Linux, but then you are restricted to the ASP.NET Core libraries that don’t depend on .NET Framework, which requires Windows.
The applications in this book require ASP.NET 1.1 with .NET Framework, which mean that you must develop on a Windows machine, or a virtual machine with Windows installed.
You will install additional libraries using NuGet packages when necessary, throughout the book. The complete code for both applications is available on GitHub with a commit for each task.
The first application: github.com/csharpschool/AspNetCoreVideo
The second application: github.com/csharpschool/VideoOnDemand

 

Other Titles by the Author

The author has written other books and produced video courses that you might find helpful.
MVC 5 – How to Build a Membership Website (video course) : This is a comprehensive video course on how to build a membership site using ASP.NET MVC 5. The course has more than 24 hours of video. In this video course you will learn how to build a membership website from scratch. You will create the database using Entity Framework code-first, scaffold an Administrator UI, and build a front-end UI using HTML5, CSS3, Bootstrap, JavaScript, C#, and MVC 5. Prereq-uisites for this course are: a good knowledge of the C# language and basic knowledge of MVC 5, HTML5, CSS3, Bootstrap, and JavaScript.

You can watch this video course on Udemy at this URL: udemy.com/building-a-mvc-5-membership-website
Store Secret Data in .NET Core Web App with Azure Key Vault (video course)
In this Udemy course you will learn how to store sensitive data in a secure manner. First you will learn how to store data securely in a file called secrets.json with the User Manager. The file is stored locally on your machine, outside the project’s folder structure. It is therefore not checked into your code repository. Then you will learn how to use Azure Web App Settings to store key-value pairs for a specific web application. The third and final way to secure your sensitive data is using Azure Key Vault, secured with Azure Active Directory in the cloud.
The course is taught using a ASP.NET Core 1.1 Web API solution in Visual Studio 2015.
You really need to know this if you are a serious developer.
You can watch this video course on Udemy at this URL: udemy.com/store-secret-data-in-net-core-web-app-with-azure-key-vault

C# for Beginners: The Tactical Guidebook : This book is for YOU if you are new to C# or want to brush up on your skills, and like a CHALLENGE. This is not your run-of-the-mill encyclopedic programming book; it is highly modularized, tactical, and practical – meaning that you learn by reading theory, and then implement targeted exercises building many applications.

 

Disclaimer – Who Is This Book for?

It’s important to mention that this book is not meant to be a get-to-know-it-all book; it's more on the practical and tactical side, where you will learn as you progress through the exercises and build real applications in the process. Because I personally dislike having to read hundreds upon hundreds of pages of irrelevant fluff (filler material) not necessary for the tasks at hand, and also view it as a disservice to the readers, I will assume that we are of the same mind on this, and will therefore only include important information pertinent for the tasks at hand, thus making the book both shorter and more condensed and also saving you time and effort in the process. Don't get me wrong, I will describe the important things in great detail, leaving out only the things that are not directly relevant to your first experience with a ASP.NET Core 1.1 MVC Web Application. The goal is for you to have created a working MVC application upon finishing this book. You can always look into details at a later time when you have a few projects under your belt. If you prefer encyclopedic books describing everything in minute detail with short examples, and value a book by how many pages it has, rather than its content, then this book is NOT for you.
The examples in this book are presented using the free Visual Studio 2017 Community version and ASP.NET Core 1.1. You can download Visual Studio 2017 here:
visualstudio.com/downloads

 

1. Your First ASP.NET Core Application

Now that you have Visual Studio 2017 installed on your computer, it’s time to create your first project.
Creating the Solution and Project
1. Open Visual Studio 2017.
2. Select File-New-Project (Ctrl+Shift+N) in the main menu.
3. Make sure that the Templates-Visual C#-Web node is selected in the New Project dialog. There are three templates available:
a. ASP.NET Web Application (.NET Framework) Can be used to create traditional ASP.NET web applications (without .NET Core).
b. ASP.NET Core Web Application (.NET Core) Can be used to create cross-platform ASP.NET Core web applications without the .NET Framework (not restricted to Windows).
c. ASP.NET Core Web Application (.NET Framework) Can be used to create ASP.NET Core web applications dependent on .NET Framework (not cross-platform).
4. Select the ASP.NET Core Web Application (.NET Framework) template.
5. Give the application a name and select which folder to place it in. You can name it AspNetCoreVideo.
6. Make sure that the Create directory for solution checkbox is checked.
7. Learning how to use GitHub is not part of this course, so if you are unfamiliar with GitHub, you should make sure that the Create new Git repository checkbox is unchecked.
8. Click the OK button.

 

لینک دانلود کتاب ASP.NET Core 1.1 MVC for Beginners.pdf

 

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