Visual Studio .Net Core tag helpers are not working

31.9k Views Asked by At

Well, lets get down to it. I'm using Visual Studio 2015 and ASP.NET core tag helpers have completely stopped working, no idea why as I've not changed anything. I was in work one day, they worked fine, then I came in the next day and now no ASP.NET core web project at all shows any tag helpers!?! I've tried creating a fresh new ASP.NET Core MVC project and they don't work there either!!! I'm completely stuck, is there some setting somewhere that completely turns them off in Visual Studio?

Note that I've checked everything that usually causes this problem according to most other posts. My _ViewImports has the line:-

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

and here is a complete project.json file from a fresh project I just created and made NO changes to at all:-

{
  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net452": { }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Also, all the _Layout views and other views have completely lost their highlighting that used to appear. No one else I've found on the internet so far seems to be having this problem, don't suppose anyone else has any ideas?

EDIT:

One of the answers below has solved this for new projects in a new solution but I'm still having problems with existing solutions/projects. Here's the project file contents from one of these existing projects, hopefully someone can spot something here...

{
  "dependencies": {
    "BundlerMinifier.Core": "2.2.306",
    "Configuration.Web": "1.0.0-*",
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Mvc.TagHelpers": "1.1.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.StaticFiles": "1.1.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools":  "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net452": {
      "dependencies": {
        "Aristotle.Service": {
          "target": "project"
        },
        "Infrastructure": {
          "target": "project"
        }
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

I've updated all packages using NuGet to latest versions...

14

There are 14 best solutions below

4
On

In your project.json dependencies, you are missing TagHelpers. Please add below line in dependencies section of project.json-

"Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.1",

On saving Project.json, VS2015 automatically restore packages.

If it doesn't worked then right click on project and click on Restore Packages option.

If this doesn't worked then try restoring using dotnet restore CLI command.

1
On

I've finally fixed this, but have no idea why the fixed works or why it stopped working in the first place but...

in _ViewImports the line which is:-

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

I've changed to include quotes:-

@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"

Then I did a rebuild and its working again!?! All the 'asp-' attributes are also now highlighted as they were before. Why?!? Eh?!?

Stranger still, if I remove the quotes and rebuild, it still works! Well, at least until my colleagues get the file out of source control, they have to put the quotes back in!!

Figure that one out...

0
On

I had a similar problem and my _ViewImports.cshtml was in my Views folder.

For me, adding an extra line at the bottom of the _ViewImports.cshtml file did the trick:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, YourProjectNameHere
1
On

Here's good contender for the dumbest way to cause Tag Helpers to ignore you in disdain. Try commenting out an individual HTML attribute...

<input id='@Model.InputId.Records[0].TenantId'
        class='class-1'
        @*class='class-2'   THIS WILL NEGATE THE asp-for ATTRIBUTE BELOW *@
        asp-for="@Model.Records[0].TenantId"
        type='text'
        placeholder='Required.' />
1
On

I had the same problem with my application. I had the reference to

@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers

In my _ViewImports.cshtml file, but this file was present inside of ~/Views/Home/ folder along with other views files, for the Views files in the same folder the Tag helper code was working correctly.

My _Layout.cshtml file was in the ~/Views/Shared/ folder. When I made a copy of the _ViewImports.cshtml file and moved the copy into the same folder with _Layout.cshtml, the Tag Helper code started to work correctly.

Then I moved the _ViewImports.cshtml to ~/Views/ and the Tag helper code started to work correctly for the views in both ~/Views/Home/ and ~/Views/Shared/ folder.

So the solution is to move the _ViewImports.cshtml file to the root of the Views folder (~/Views/) so it can be referenced by all the other Views files.

0
On

In my case , I added viewimport.cshtml to Pages=>shared and it didn't work . I moved it to Pages and now it's available for all views .So it works.

0
On

I have this project.json for today:

{
  "dependencies": {
    "BundlerMinifier.Core": "2.4.337",
    "Microsoft.ApplicationInsights.AspNetCore": "2.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.1.2",
    "Microsoft.AspNetCore.Mvc": "1.1.3",
    "Microsoft.AspNetCore.Mvc.TagHelpers": "1.1.3",
    "Microsoft.AspNetCore.Razor.Design": "1.1.0-preview4-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
    "Microsoft.AspNetCore.Routing": "1.1.2",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.2",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.2",
    "Microsoft.AspNetCore.StaticFiles": "1.1.2",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.2",
    "Microsoft.Extensions.Configuration.Json": "1.1.2",
    "Microsoft.Extensions.Logging": "1.1.2",
    "Microsoft.Extensions.Logging.Console": "1.1.2",
    "Microsoft.Extensions.Logging.Debug": "1.1.2",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.1.1"
    },
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0",
    //"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final",
    //"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.1.1",
    "Wallet.Core": "1.0.0-*"
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": [
        "dnxcore50"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}
0
On

If you are experiencing the same problem when using Areas, add the _ViewImports file to the Views folder of your Area

4
On

For anyone meet the same problem, please check the location of the _ViewImports.cshtml, It must be in the same folder of your Views. For example, I have created a project that already have the _ViewImports.cshtml file in Pages folder, but I create and use my view files located in another folder, so ASP.NET can't find the _ViewImports.cshtml for our views.

With anyone have issue finding the project.json, you can right click at the project and choose Manage Nuget Packages then install the Microsoft.AspNetCore.Mvc.TagHelpers.

Hope these advices can help everyone.

0
On

Some time there is an issue related to scope of _ViewImports.cshtml if we have multiple _layout files in our Views Folder.

You can add a _ViewImports.cshtml to any view folder, and the view engine applies the directives from both that file and the Views/_ViewImports.cshtml file. If you added an empty Views/Home/_ViewImports.cshtml file for the Home views, there would be no change because the _ViewImports.cshtml file is additive. Any @addTagHelper directives you add to the Views/Home/_ViewImports.cshtml file (that are not in the default Views/_ViewImports.cshtml file) would expose those Tag Helpers to views only in the Home folder.

1
On

For those w/ a ViewComponent inside a Razor Class Library (RCL), in addition to some of the other posted suggestions, 3 additional tweaks resolved my un-reachable Tag Helper for me:

  1. The _ViewImports file should include an @using directive that points to your tag helper NAMESPACE (even though intellisense will think it's not needed)
  2. The _ViewImports reference s/b to @addTagHelper *, <DLL NAME> - NOT necessarily the namespace name
  3. Besides under the web app root directory, the _ViewImports file needs to also exist in the root of the RCL project - but is not needed w/in MVC "Areas" in the RCL nor any "Views" folder
0
On

in my core 3.1 project, the problem turned out to be due to the connected Microsoft.Office.Interop.Word

<ItemGroup>
     <COMReference Include="Microsoft.Office.Interop.Word">
       <WrapperTool>tlbimp</WrapperTool>
       <VersionMinor>6</VersionMinor>
       <VersionMajor>8</VersionMajor>
       <Guid>00020905-0000-0000-c000-000000000046</Guid>
       <Lcid>0</Lcid>
       <Isolated>false</Isolated>
       <EmbedInteropTypes>true</EmbedInteropTypes>
<Private>true</Private>
     </COMReference>
   </ItemGroup>

after removing Microsoft.Office.Interop.Word everything worked, now I'm looking for something to replace Microsoft.Office.Interop.Word

0
On

If you already have that _ViewImports.cshtml file If you have views in other folder structure like "WebHost/Features/MyFolder/Views/SampleView.cshtml" and not in WebHost/Views

Then move your _ViewImports.cshtml file in the project folder. Like WebHost/_ViewImports.cshtml

Now it should work for all views.

0
On

I noticed that the tag helpers only helped for some views as @MayBeNextTime kindly informed us. Although, @Menace advises to add duplicates of the _ViewImports file to every Area you have views in, in order for the tag helpers to work for every view you have in your application, I found that by moving the _ViewImports file to the main application folder (right next to/or above Program.cs , Startup.cs , appsettings.json in your Solution Explorer) that you will achieve ubiquitous tag helpers' functionality without creating duplicate files in your app.

Check this out:

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/layout?view=aspnetcore-2.2

It says:

A _ViewImports.cshtml file can be placed within any folder, in which case it will only be applied to pages or views within that folder and its subfolders.