Visual Studio .net core tag helpers not working

后端 未结 9 2440
暗喜
暗喜 2021-02-19 00:49

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 o

相关标签:
9条回答
  • 2021-02-19 01:20

    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 讨论(0)
  • 2021-02-19 01:20

    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 讨论(0)
  • 2021-02-19 01:20

    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 讨论(0)
  • 2021-02-19 01:22

    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 讨论(0)
  • 2021-02-19 01:23

    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 讨论(0)
  • 2021-02-19 01:32

    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.

    0 讨论(0)
提交回复
热议问题