J.Tian J.Tian - 4 months ago 11
HTML Question

How to use HTML links in ASP.Net Core MVC?

I'm currently trying to make a website in ASP.NET Core MVC. In my layout page, I'm making a navigation bar to access all of the actions that can be reached through my controllers. I am however unable to create useful links.

<ul>
<li><a href="homepage">Home</a></li>
<li><a href="index">Index</a></li>
</ul>


My problem with this is that I still need the controller before the links and if I put the controller in front of the action like this

<ul>

<li><a href="home/homepage">Home</a></li>
<li><a href="home/index">What We've Done</a></li>
</ul>


When I click on one link and then the other, the link will end up being "myurl/home/home/page".

How can I create the link to only link to the exact page I want to?

Answer

You should use the anchor tag helper to build the markup for the link

<a asp-action="index" asp-controller="home">Home</a>

This will generate the correct relative path to the index action as the href property value of the anchor tag.

Comments