Managing namespaces in views with ViewImports
In this recipe, you will learn how to create and manage namespaces in Razor views by creating a _ViewImport view to add all the necessary namespaces we need in our application views.
Getting ready
We create an empty web application with a controller and an index view with the corresponding folders.
How to do it...
The goal of TagHelpers is to reduce the writing of the views using HTML tags and/or attributes interpreted by Razor. They replace HTMLHelpers with the closest syntax of HTML, allowing integrators to work with MVC views more easily:
- First, let's add an
Insertaction method forGETandPOSTinHomeController.cs, and let's add a break point on theInsertmethod ofPOST:

- Next, let's add the
Insertview with TagHelper code for an input to test TagHelpers:

- We can see the
asp-forTagHelper added to the input text HTML element.
- Let's test the
POSTmethod, adding a value to the input:

- We click on the
Sendbutton to see what happened in theInsertmethod...