Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 901
J.Tian Avatar asked Jul 06 '16 18:07

J.Tian


Video Answer


1 Answers

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.

like image 86
Shyju Avatar answered Sep 21 '22 00:09

Shyju