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 can use the environment tag to specify the environment the libraries should be accessible from. You usually want the un-minified libraries in the included Development <environment> Tag Helper for easy debugging, and the minified versions in the excluded Development <environment> Tag Helper for production builds.
Many times, you can achieve even faster load times if you use Content Delivery Networks (CDNs), servers that have cached versions of the libraries that can be called. You can find information about the Microsoft’s CDNs at:
https://docs.microsoft.com/en-us/aspnet/ajax/cdn/overview.
Four attributes are used to check that the JavaScript libraries have been installed: asp-fallback-test-class, asp-fallback-test-property, asp-fallback-test-value, and asp-fallback-test.
Two of the attributes are used to load an alternative source if the JavaScript libraries haven’t been installed: asp-fallback-src and asp-fallback-href.
app.UseStaticFiles();
<environment include="Development">
</environment>
<environment exclude="Development">
</environment>
<environment include="Development">
<link href="~/lib/twitter-bootstrap/css/bootstrap.css" rel="stylesheet" />
</environment>
<environment exclude="Development">
<link href="~/lib/twitter-bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</environment>
<environment include="Development">
<script src="~/lib/jquery/jquery.js"></script>
<script src="~/lib/jquery-validate/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
<script src="~/lib/twitter-bootstrap/js/bootstrap.js"></script>
</environment>
<environment exclude="Development">
<script src="~/lib/jquery/jquery.min.js"></script>
<script src="~/lib/jquery-validate/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script src="~/lib/twitter-bootstrap/js/bootstrap.min.js"></script>
</environment>
The complete dependency list in the libman.json file:
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": [
{
"library": "[email protected]",
"destination": "wwwroot/lib/jquery/"
},
{
"library": "[email protected]",
"destination": "wwwroot/lib/twitter-bootstrap/"
},
{
"provider": "cdnjs",
"library": "[email protected]",
"destination": "wwwroot/lib/jquery-validation-unobtrusive/"
},
{
"provider": "cdnjs",
"library": "[email protected]",
"destination": "wwwroot/lib/jquery-validate/",
"files": ["jquery.validate.min.js"]
}]
}
Let’s add a navigation bar in the _Layout view, and style it and the video list in the Index view, using Bootstrap.
The navigation bar for an anonymous user:
The navigation bar for a logged in user:
<div>
<partial name="_LoginLinks"/>
</div>
<nav class="navbar navbar-expand-lg navbar-light bg-light"></nav>
<a class="navbar-brand" href="/">Video Application</a>
<button class="navbar-
toggler" type="button"
data-toggle="collapse" data-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown"></div>
<ul class="navbar-nav ml-auto"></ul>
<partial name="_LoginLinks" />
<form method="post" asp-controller="Account" asp-action="Logout">
@User.Identity.Name
<input type="submit" value="Logout" />
</form>
<input type="submit" value="Logout" class="btn btn-danger" />
<li class="nav-item">
<a asp-controller="Account" asp-action="Login" class="btn btn-outline-dark mr-sm-1">Login</a>
</li>
<li class="nav-item">
<a asp-controller="Account" asp-action="Register" class="btn btn-outline-success">Register</a>
</li>
The complete markup for the _LoginLinks partial view:
@using Microsoft.AspNetCore.Identity
@inject SignInManager<VideoUser> SignInManager
@inject UserManager<VideoUser> UserManager
@if (SignInManager.IsSignedIn(User))
{
// Signed in user
<form method="post" asp-controller="Account" asp-action="Logout">
@User.Identity.Name
<input type="submit" value="Logout" class="btn btn-danger"/>
</form>
}
else
{
// Anonymous user
<li class="nav-item">
<a asp-controller="Account" asp-action="Login" class="btn btn-outline-dark mr-sm-1">Login</a>
</li>
<li class="nav-item">
<a asp-controller="Account" asp-action="Register" class="btn btn-outline-success">Register</a>
</li>
}
The navigation bar markup in the _Layout view:
<nav class="navbar navbar-expand-sm navbar-light bg-light">
<a class="navbar-brand" href="/">Video Application</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav ml-auto">
<partial name="_LoginLinks" />
</ul>
</div>
</nav>
Let’s make the video list a bit more appealing by adding Bootstrap classes to make them appear like cards. The image below shows the end result.
<div class="container-fluid">
@RenderBody()
</div>
<a class="btn btn-success" asp-action="Create">Create</a>
<div class="card-deck mt-3"></div>
<div class="card border-success mb-3"></div>
<div class="card-header bg-success border-success">@Model.Title</div>
<div class="card-body"></div>
<p class="card-text">@Model.Genre</p>
<a asp-action="Details" asp-route-id="@Model.Id">Details</a>
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a>
The complete markup for the _VideoPartial view:
@model VideoViewModel
<div class="card border-success mb-3">
<div class="card-header bg-success border-success">@Model.Title</div>
<div class="card-body">
<p class="card-text">@Model.Genre</p>
<a asp-action="Details" asp-route-id="@Model.Id">Details</a>
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a>
</div>
</div>
To take advantage of client-side validation, you only have to add the jquery, jquery.
validate, and jquery.validate.unobtrusive JavaScript libraries. You have already added the libraries in a previous section, so now you only have to check that the validation works.
Pay attention to the URL field in the browser as you click the Login, Edit, and Create buttons when you try to enter invalid data (you can for instance leave one of the text fields empty). The URL should not refresh, because no round-trip is made to the server.
In this chapter, you used Bower to add JQuery libraries to enforce client-side validation, and Bootstrap to style the Index view with CSS classes.
Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.
50% Complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.