According to the documentation:
The
@addTagHelper
directive makes Tag Helpers available to the view. In this case, the view file isViews/_ViewImports.cshtml
, which by default is inherited by all view files in the Views folder and sub-directories; making Tag Helpers available.
So I imported the Tag Helpers in the Views\_ViewImports.cshtml
:
@using MyProject
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
and they work well on files inside the Views
folder.
However, in the file Views/Home/Index.cshtml
, I don't have support for Tag Helpers, neither are they correctly rendered as links. When I copy the _ViewImports.cshtml
into the folder Views/Home
, everything works as expected.
So what am I missing?
Update
So what am I missing? That my _ViewImports.cshtml
was put in the folder Views/Shared
(d'oh). After moving it to Views
, the TagHelpers work as intended everywhere.
The first step when a tag helper isn't working is always to check the rendered HTML. Tag Helpers should be converted to proper HTML – if they're actually sat on the page as a tag helper then nothing will happen, because Chrome and Firefox don't know what the hell a tag helper is.
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.
A Tag Helper Component is a Tag Helper that allows you to conditionally modify or add HTML elements from server-side code. This feature is available in ASP.NET Core 2.0 or later. ASP.NET Core includes two built-in Tag Helper Components: head and body . They're located in the Microsoft. AspNetCore.
I tried a blank new ASP.NET Core 1.0 project and add it is working fine under Views/Home/Index.cshtml
view. This is what you have to do (Make sure that it is available):
Add to following package and tool section into your project.json
file:
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
}
},
and
"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
Create an _ViewImports.cshtml
file under Views folder (You have one already) with the contents that you have mentioned.
Note: You may have to restart your VS to get it working.
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