5

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 Commented May 16, 2016 at 9:05

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.