I spent the last hour refactoring to use Areas, now all my Views don't seem to have function taghelpers :/
So this is what's in the Index.cshtml
<div class="btn-group">
<a asp-controller="Survey" asp-area="Admin" asp-action="Create" class="btn btn-primary">Create New</a>
</div>
...and this is the rendered HTML :/
<div class="btn-group">
<a asp-controller="Survey" asp-area="Admin" asp-action="Create" class="btn btn-primary">Create New</a>
</div>
Intellisense doesn't even show the asp- prefixes and syntax highlighting on the asp- attributes is also lost.
Other SO issues reference "asp-route-area" but that just renders out verbtim like the rest.
These all worked fine when they were in ~/Views/Name/Index.cshtml, move them out to ~/Areas/Name/Views/Name/ and nopers...
Any thoughts? Steve
There are many wonderful things that you can do with a tag helper, but you need to register your tag helpers with Razor, even the Microsoft tag helpers, in order for Razor to be able to spot these tag helpers in the markup and to be able to call into the code that processes the tag helper.
Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.
The opt-out character (“!”) is used to disable the Tag Helper at the element level. With the opt-out character, the HTML will not be generated for the label tag in the above case. We can use this opt-out character if we want to conditionally control rendering of the HTML elements.
@addTagHelper makes Tag Helpers available cshtml , which by default is inherited by all files in the Pages folder and subfolders; making Tag Helpers available. The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft. AspNetCore.
If you create a new ASP.NET Core web app named AuthoringTagHelpers, the following Views/_ViewImports.cshtml file will be added to your project: The @addTagHelper directive makes Tag Helpers available to the view.
In many cases, HTML Helpers provide an alternative approach to a specific Tag Helper, but it's important to recognize that Tag Helpers don't replace HTML Helpers and there's not a Tag Helper for each HTML Helper. Tag Helpers compared to HTML Helpers explains the differences in more detail.
The @addTagHelper makes the built-in tag helpers to available in the application which are defined in an assembly called Microsoft.AspNetCore.Mvc.TagHelpers. Here the wildcard “*” specifies that all the Tag Helpers are made available. Let us understand this with an example.
In order to use Tag Helpers first import the @addTagHelper directive in the _ViewImport.cshtml file. Along with the @addTagHelper directive, we also add the model namespace using the @using directive. So, modify the _ViewImport.cshtml file as shown below which you can find within the Views folder. Then modify the Index view as shown below.
According to official docs:
The @addTagHelper directive makes Tag Helpers available to the view. In this case, the view file is Views/_ViewImports.cshtml, which by default is inherited by all view files in the Views folder and sub-directories; making Tag Helpers available. The code above uses the wildcard syntax (“*”) to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.
If you use one layout per area, to use built-in tag helpers you should add _ViewImports.cshtml
in ~/Areas/Name/Views/
folder(If you use shared layout you don't need. See MusicStore project for shared layout example).
I guess you used one layout per area and you didn't add _ViewImports.cshtml
~/Areas/Name/Views/
. Copy /Views/_ViewImports.cshtml
into ~/Areas/Name/Views/
.
As it turns out, adding the _ViewImports.cshtml
file to the Areas/
folder cascades the visibility of the file to all Areas/{area}/views
within the folder.
So rather than:
-> Areas
--> Area1
----> Views
------> _ViewImports.cshtml
--> Area2
----> Views
------> _ViewImports.cshtml
We can just do:
-> Areas
--> Area1
--> Area2
--> _ViewImports.cshtml
Or more visually
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With