concept Tag Helpers in category asp.net

appears as: Tag Helpers, Tag Helpers, The Tag Helpers
ASP.NET Core in Action

This is an excerpt from Manning's book ASP.NET Core in Action.

Listing 7.13. A typical _ViewImports.cshtml file importing additional namespaces
@using WebApplication1 #1 @using WebApplication1.Models #2 @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers #3 #1 The default namespace of your application #2 Add this directive to avoid placing in every view #3 Makes Tag Helpers available in your views !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":57},{\"line\":0,\"ch\":59}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":29}],[{\"line\":1,\"ch\":57},{\"line\":1,\"ch\":59}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":2,\"ch\":57},{\"line\":2,\"ch\":59}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}]]"} !@%STYLE%@!

In the next chapter, you’ll learn about Tag Helpers and how to use them to build HTML forms, a staple of modern web applications. Tag Helpers are one of the biggest improvements to Razor in ASP.NET Core, so getting to grips with them will make editing your views an overall more pleasant experience!

Tag Helpers are new to ASP.NET Core. They’re Razor components that you can use to customize the HTML generated in your templates. Tag Helpers can be added to an otherwise standard HTML element, such as an <input>, to customize its attributes based on your view model, saving you from having to write boilerplate code. They can also be standalone elements and can be used to generate completely customized HTML.

Remember, Razor, and therefore Tag Helpers, are for server-side HTML rendering. You can’t use Tag Helpers directly in frontend frameworks like Angular or React.

If you’ve used the previous version of ASP.NET, then Tag Helpers may sound reminiscent of HTML Helpers, which could also be used to generate HTML based on your view model. Tag Helpers are the logical successor to HTML Helpers, as they provide a more streamlined syntax than the previous, C#-focused helpers. HTML Helpers are still available in ASP.NET Core, so if you’re converting some old templates to ASP.NET Core, or want to, you can use them in your templates, but I won’t be covering them in this book.

In this chapter, you’ll primarily learn how to use Tag Helpers when building forms. They simplify the process of generating correct element names and IDs so that model binding can occur seamlessly when the form is sent back to your application. To put them into context, you’re going to carry on building the currency converter application that you’ve seen in previous chapters. You’ll add the ability to submit currency exchange requests to it, validate the data, and redisplay errors on the form using Tag Helpers to do the leg work for you, as shown in figure 8.1.

Figure 8.1. The currency converter application forms, built using Tag Helpers. The labels, dropdowns, input elements, and validation messages are all generated using Tag Helpers.

As you develop the application, you’ll meet the most common Tag Helpers you’ll encounter when working with forms. You’ll also see how you can use Tag Helpers to simplify other common tasks, such as generating links, conditionally displaying data in your application, and ensuring users see the latest version of an image file when they refresh their browser.