One of the big advantages of using a mature stack like .NET is that the ecosystem of third-party packages and plugins is huge. Just like other package systems (npm, Maven, RubyGems), you can download and install .NET packages that help with almost any task or problem you can imagine.
NuGet is both the package manager tool and the official package repository (at https://www.nuget.org). You can search for NuGet packages on the web, and install them from your local machine through the terminal (or the GUI, if you're using Visual Studio).
At the end of the last chapter, the to-do application displayed to-do items like this:
The due date column is displaying dates in a format that's good for machines (called ISO 8601), but clunky for humans. Wouldn't it be nicer if it simply read "X days from now"? You could write code that converted a date into a human-friendly string, but fortunately, there's a faster way.
The Humanizer package on NuGet (https://www.nuget.org/packages/Humanizer) solves this problem by providing methods that can "humanize" or rewrite almost anything: dates, times, durations, numbers, and so on. It's a fantastic and useful open-source project that's published under the permissive MIT license.
To add it to your project, run this command in the terminal:
dotnet add package Humanizer
If you peek at the
AspNetCoreTodo.csproj project file, you'll see a new
PackageReference line that references
To use a package in your code, you usually need to add a
using statement that imports the package at the top of the file.
Since Humanizer will be used to rewrite dates rendered in the view, you can use it directly in the view itself. First, add a
@using statement at the top of the view:
@model TodoViewModel @using Humanizer // ...
Then, update the line that writes the
DueAt property to use Humanizer's
Now the dates are much more readable:
There are packages available on NuGet for everything from parsing XML to machine learning to posting to Twitter. ASP.NET Core itself, under the hood, is nothing more than a collection of NuGet packages that are added to your project.
The project file created by
dotnet new mvcincludes a single reference to the
Microsoft.AspNetCore.Allpackage, which is a convenient "metapackage" that references all of the other ASP.NET Core packages you need for a typical project. That way, you don't need to have hundreds of package references in your project file.
In the next chapter, you'll use another set of NuGet packages (a system called Entity Framework Core) to write code that interacts with a database.