1
votes

I have a problem with TagHelpers, they are not being resolved.

I have added Microsoft.AspNetCore.Mvc.TagHelpers to the project using NuGet, and it is showing as installed.

I have the _ViewImports.cshtml file, containing...

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

... in my Views/Shared folder.

The Index.cshtml view file has an entry in the head element...

<link rel="stylesheet" asp-href-include="lib/bootstrap/dist/css/*.min.css" />

The css files are in the referenced directory.

I have added...

 app.UseStaticFiles();

... to the Configure() startup function.

ConfigureServices() also includes...

 services.AddMvc();

When I run the app, and do a View Source on the received html, I can see the asp-href-include tag helper has not been resolved, it appears as it is in the cshtml file. There are no errors, and no exceptions occur.

I have tried restarting VS 2017, but no joy.

What have I missed that would stop tag helpers working but not generate errors? Also, how can I debug this?

1
Are those css files available under wwwroot? Try changing it to - <link rel="stylesheet" asp-href-include="~/lib/bootstrap/dist/css/*.min.css" /> - Sanket
@Sanket No joy I'm afraid, still no conversion of the tags. Thanks for the suggestion. - Steve Hibbert
Gosh golly darn it (fu.....), I had the _ViewImports.cshtml file in the Shared folder, but it does not go there, it goes in Views. Four hours down the tubes... - Steve Hibbert
Also, if link element has an error in the folder, but tag helpers are active, the whole link element is omitted from the emitted markup. In this way, you can tell if you have a typo in your links to CSS or JS files. - Steve Hibbert

1 Answers

2
votes

I had my _ViewImports.cshtml file in the /Views/Shared folder (which looks sane, right?).

However, this file must go in the /Views folder. (Note, each view can have it's own copy in a local folder).

Effectively, the @addTagHelpers directive was being ignored, because the _ViewImports.cshtml file was in the wrong place and not being picked up.

Solution was to copy the file up one directory from /Views/Shared to /Views.