5
votes

When I use asp-controller and asp-action in a <a> tag for another Action than the current, in a View called by a Controller Method with a [Route] attribute, the generated link have an empty href attribute.

In the Controller:

public class ForumController : Controller
{
    [Route("[action]/{sectionId:int}")]
    public async Task<IActionResult> ShowSection(int sectionId)
    {
        //some code
    }
}

In the View:

<a asp-controller="Forum" asp-action="Index">Index</a>
<a asp-controller="Forum" asp-action="ShowSection" asp-route-sectionId="@Model.ParentSection.Id">@Model.ParentSection.Name</a>

Generated html:

<a href="">Index</a>
<a href="/ShowSection/1">Général</a>

As you can see, the first link is not generated correctly. All of the links that target another Action than the currenct Action are generated with an empty href tag.

When I remove the [Route] attribute of the ShowSection action:

<a href="/Forum">Index</a>
<a href="/Forum/ShowSection?sectionId=1">Général</a>

As you can see, the links are correctly generated.

How can I fix this while keeping my [Route] attributes (or with an alternative)?

1
Show your Index() action along with the routing registration in your Startup.cs - haim770

1 Answers

0
votes

I finally found it (@haim770 comment helped) :

I added a Route attribute to all my controllers & actions and now it works.