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

风流意气都作罢 提交于 2019-12-01 17:49:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38231135/how-to-use-html-links-in-asp-net-core-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!