简体   繁体   中英

Anchor Tag helper not producing href in .NET 5

I was using Anchor tag helper in .net5 but it is not producing expected href. I have tried to ask many people but no help.

<a asp-controller="home" asp-action="details" asp-route-id="@employee.Id" class="btn btn-primary">View</a>

produces

<a class="btn btn-primary" href="">View</a>

but It has to produce <a class="btn btn-primary" href="/home/details/1">View</a>

I am attaching following screenshots with it

index view page with tag helper code

_ViewImports file

Solution Explorer

Page Source on running

Two things, you need to check, firstly the asp.net core tag helper will generate the hert url based on your application route, please check your program.cs or startup.cs to make sure you have set the controller route well.

Like below:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

If you use the wrong route or else, it will not generate the herf value like below:

app.MapControllerRoute(
    name: "default",
    pattern: "");

Result:

在此处输入图像描述

The second thing is, you should check your anohter tag helper to make sure it contains the another anchor tag helper which caused this issue. You could try to comment it inside the viewimports to test it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM