Asp.Net Core Tag Helpers not available in Sub-Directory
Asked Answered
I

1

7

According to the documentation:

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.

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.

Ingridingrim answered 14/7, 2016 at 15:34 Comment(2)
It works for me. If you have created your own taghelper then you have to create entry for that in _ViewImports.cshtml file.Dialogism
I want to use the helpers defined by Microsoft, namely asp-controller and asp-action.Ingridingrim
F
4

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.

Fino answered 15/7, 2016 at 9:56 Comment(1)
Yes, it works for me when I put the file in the right folder (see update to my question). Thanks!Ingridingrim

© 2022 - 2024 — McMap. All rights reserved.