如何在ASP.Net Core MVC中使用HTML链接? [英] How to use HTML links in ASP.Net Core MVC?
问题描述
我目前正在尝试在ASP.NET Core MVC中创建一个网站。在我的布局页面中,我制作了一个导航栏,以访问可通过我的控制器访问的所有操作。然而,我无法创建有用的链接。
< ul>
< li>< a href =homepage>主页< / a>< / li>
< li>< a href =index>索引< / a>< / li>
< / ul>
我的问题是,我仍然需要控制器之前的链接,如果我把控制器这样的行动的前面
< ul>
< li>< a href =家/主页>主页< / a>< / li>
< li>< a href =home / index>我们完成了什么< / a>< / li>
< / ul>
当我点击一个链接,然后点击另一个链接时,链接最终会成为myurl / home / home / page。
如何创建链接以链接到我想要的确切页面?
您应该使用锚标记帮助程序为链接构建标记
< a asp-action =indexasp-controller =home>主页< / a>
这将生成正确的相对于index行为的相对路径,作为锚标记的href属性值。
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.
这篇关于如何在ASP.Net Core MVC中使用HTML链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!