In this chapter, you will learn how to install front-end libraries using LibMan. LibMan wasn’t developed to replace package managers, such as npm or yarn. If your project uses a package manager then continue to do so. LibMan is available from Visual Studio 2017 version 15.8. You can follow the installation instructions if you want to add client-side libraries locally in your project; this is not necessary in this project since you have added CDN links in the _Layout file.
Bootstrap: This is the most popular library for styling and responsive design. You will use some Bootstrap CSS classes to style the video list and the navigation links. You can find out more about Bootstrap on their site: http://getbootstrap.com.
LibMan is the preferred way to install front-end frameworks in ASP.NET Core 2.2. When the libraries have been installed, they must be referenced from the _Layout view for global access, or in individual views for local access. You...
In this chapter, you will learn about ASP.NET Identity and how you can use it to implement registration and login in your application. You will add the authentication from scratch to learn how all the pieces fit together.
ASP.NET Identity is a framework that you need to install either with the NuGet Manager or by adding it manually in the .csproj file. It can handle several types of authentication, but in this chapter, you will focus on Forms Authentication.
The first thing you need to add is a User entity class that inherits from an identity base class, which gives you access to properties such as Username, PasswordHash, and Email. You can add as many properties to the User class as your application needs, but in this chapter, you will only use some of the inherited properties.
The User class needs to be plugged into a class called UserStore, provided by the Identity framework. It is used when creating and validating a user that then is sent to a database; Entity Framework is...
In this chapter, you will learn about different views that can be used for layout, to include namespaces, and to render partial content in a view.
The _Layout.cshtml Razor view gives the application more structure and makes it easier to display data that should be visible on every page, such as a navigation bar and a footer. You avoid duplication using this view. The underscore at the beginning of the name is not required, but it is a convention that is commonly used among developers. It signifies that the view shouldn’t be rendered as a view result with the View method from a controller action.
The normal views, like the Index view, are rendered inside the _Layout view. This means that they don’t have any knowledge about the navigation and the footer; they only need to render what the action tells them to render.
If you look inside the views you have created, they have some code in common, such as the <html>, <head>, and <body> elements....
In this chapter, you will set up Entity Framework (EF) and get familiar with how it works. To work with EF, you must install the proper services, either manually in the .csproj file or by using the NuGet manager.
When the services have been installed and configured in the Startup class, you need to add a data context class that inherits from the DbContext class. This class will be the context that you use to interact with the database. To add a table to the database, the table’s entity class must be added as a DbSet property in the context class.
When the services are installed and configured in the Startup class, you create the first migration by using the Package Manager Console and the Add-Migration command. When the initial migration has been added, the database can be generated with the Update-Database command.
If you make any changes to the database, like adding or changing columns or tables, then you must execute the Add-Migration and Update-Database commands again...
Data annotations are attributes you add to properties in a model, to enforce rules about them. You can specify that a field is required or must have a maximum number of characters. The text displayed in a label is normally the property name, but that can be overridden with the [Display] attribute.
Many data annotations can be found in the System.ComponentModel.DataAnnotations namespace. You can specify one annotation per code line, or multiple annotations as a comma-separated list inside a set of square brackets.
[Required]
[MaxLength(80)]
Or
[Required, MaxLength(80)]
Below is a list of commonly used data annotations.
To validate the annotations in the browser, the view must be altered to display possible errors. You usually do this by adding a <span> or a <div> element decorated with the asp-validation-for Tag Helper, specifying which property it displays errors for. You can also add a...
When creating a new record in the data source with a Create view, you have to implement two action methods. The first is a method using HTTP GET to render the Create view in the browser, filling select lists and other controls that need data. The second method is an HTTP POST method that receives data from the client through an HTTP POST request.
The post from the client can be done in several ways, for instance with JavaScript or a form post. In this example, you will use a form post to call back to the server when the user clicks a Submit button.
The HTTP POST action method can fetch data from several places in the posted data: the header, the query string, and the body of the request. The data is then matched against properties in a model object, which is a parameter of the action method. The action can also handle simple types such as int and string, without them being encapsulated in a model object.
There is a naming convention that you need to be aware of,...
In this chapter, you will learn more about different types of model classes in the MVC framework and how to use them.
Up until now, you have used the Video class as a model for the Index view. In simple solutions that might be fine, but in more complex solutions, you need to use entity models and view models. Sometimes you even make a more granular distinction between the models, using Data Transfer Objects (DTOs) with the view models.
An entity model is typically used to define a table in a database. A view model is used to transport data from the controller to the view, but sometimes the view model needs to contain objects, and that’s where the DTOs come into play. Some might argue that DTOs are view models, and in some scenarios they are.
You will create a new folder called Entities and move the Video class to that folder. The reason for moving the file is that the Video class later will be used to define a table in a SQL Server database. A class used to define a database...
In this chapter, you will learn about MVC, which is a popular design pattern for the user interface layer in applications, where M stands for Model, V stands for View, and C stands for Controller. In larger applications, MVC is typically combined with other design patterns, like data access and messaging patterns, to create a full application stack. This book will focus on the MVC fundamentals.
The controller is responsible for handling any HTTP requests that come to the application. It could be a user browsing to the /videos URL of the application. The controller’s responsibility is then to gather and combine all the necessary data and package it in model objects, which act as data carriers to the views.
The model is sent to the view, which uses the data when it’s rendered into HTML. The HTML is then sent back to the client browser as an HTML response.
The MVC pattern creates a separation of concerns between the model, view, and controller. The sole responsibility...
In this chapter, you will add middleware that handles HTTP requests, like how the application behaves if there is an error. One key aspect of the middleware is to perform user authentication and authorization.
By the end of this chapter you will have built a middleware pipeline for a MVC application.
Let’s have a look at how middleware works and what it is used for.
When an HTTP request comes to the server, it is the middleware components that handle that request.
Each piece of middleware in ASP.NET is an object with a very limited, specific, and focused role. This means that you will have to add many middleware components for an application to work properly.
The following example illustrates what can happen when an HTTP POST request to a URL, ending with /reviews, reaches the server.
Logging is a separate middleware component that you might want to use to log information about every incoming HTTP request. It can see every piece of...
Instead of using one specific source to fetch data, you can use services to fetch data from different sources, depending on the circumstance. This mean that you, through the use of configuration, can use different data sources according to the need at hand.
You might want to fetch data from a JSON file when building the service, and later switch to another implementation of that service, to fetch real data.
To achieve this, you create an interface that the service classes implement, and then use that interface when serving up the instances. Because the service classes implement the same interface, instances from them are interchangeable.
To get access to the services from the Configure method in the Startup class, or any other constructor, model, Razor Page, or view, you must use dependency injection. That is, pass in the interface as a parameter to the method.
You must register the service interface, and the desired service class, with the services collection in...
50% Complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.