简体   繁体   中英

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?

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.

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