Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 960
Thaoden Avatar asked Jul 14 '16 15:07

Thaoden


People also ask

Why tag helpers is not working?

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.

What is the difference between HTML helpers and tag helpers?

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.

What are tag helpers in asp net core?

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.


1 Answers

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.

like image 61
Janshair Khan Avatar answered Nov 24 '22 11:11

Janshair Khan