问题
My link tag helpers are no longer working properly after migrating to asp.net core 2.2.
<a class="btn btn-outline-primary" asp-controller="MyController" asp-action="MyAction" asp-route-id="@Id">Link</a>
This works fine when I set the compatibility version to 2.1, but produces an empty href when set to compatibility version 2.2.
<a class="btn btn-outline-primary" href="">Link</a>
I followed the steps Migrate from ASP.NET Core 2.1 to 2.2
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1); --> Works
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2); --> Doesn't work
Project file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TypeScriptToolsVersion>2.8</TypeScriptToolsVersion>
<LangVersion>7.2</LangVersion>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.2" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.1" />
<PackageReference Include="Sendgrid" Version="9.10.0" />
<PackageReference Include="Stripe.net" Version="22.8.1" />
<PackageReference Include="UAParser" Version="3.1.36" />
</ItemGroup>
<ItemGroup>
<Folder Include="Data\Migrations\" />
</ItemGroup>
</Project>
回答1:
I am pretty new in this technology, and I had the same problem. After I added
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, MyNameSpace
in ViewImports.cshtml, as mentioned here https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring?view=aspnetcore-2.2 everything was fine.
回答2:
I had the same issue today on a new AspNet Core 2.2 Project created using the built in MVC Template. The tags were not generated in the HTML.
changing the following
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
to
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Works and the tags are now rendered in the HTML, I done some digging and it looks as though this has been raised as an issue in AspNetCore 2.2. They mention it has been fixed in AspNetCore 3.0.0-preview3.
As mentioned here: https://github.com/aspnet/AspNetCore/issues/5055 and here https://github.com/aspnet/AspNetCore/issues/6471
If you need to use AspNetCore 2.2 then a temporary workaround, if viable is to ammend the following in your Startup.cs
as mentioned on the issue raised.
services.AddMvc(options => options.EnableEndpointRouting = false)
来源:https://stackoverflow.com/questions/54722462/link-tag-helper-not-working-in-asp-net-core-2-2