Visual Studio .net core tag helpers not working

2020-07-02 09:59发布

问题:

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...

回答1:

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...



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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%" ]
  }
}


回答6:

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://docs.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.