text stringlengths 0 13.4k |
|---|
<li> |
<a asp-controller="Todo" asp-action="Index">My to-dos</a> |
</li> |
The asp-controller and asp-action attributes on the <a> element |
are called tag helpers. Before the view is rendered, ASP.NET Core |
replaces these tag helpers with real HTML attributes. In this case, a URL |
to the /Todo/Index route is generated and added to the <a> element |
43 |
Update the layout |
as an href attribute. This means you don't have to hard-code the route |
to the TodoController . Instead, ASP.NET Core generates it for you |
automatically. |
If you've used Razor in ASP.NET 4.x, you'll notice some syntax |
changes. Instead of using @Html.ActionLink() to generate a link |
to an action, tag helpers are now the recommended way to create |
links in your views. Tag helpers are useful for forms, too (you'll see |
why in a later chapter). You can learn about other tag helpers in |
the documentation at https://docs.asp.net. |
44 |
Add external packages |
Add external packages |
One of the big advantages of using a mature ecosystem like .NET is that |
the number of third-party packages and plugins is huge. Just like other |
package systems, 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). |
Install the Humanizer package |
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 yourself that converted an ISO 8601 date into a |
human-friendly string, but fortunately, there's a faster way. |
The Humanizer package on NuGet 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 |
45 |
Add external packages |
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 Humanizer . |
Use Humanizer in the view |
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. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.