I have this:
<li><a href="/Users/Index)" class="elements"><span>Clients</span></a></li>
Which works fine. But if I am already on this page or on the controller e.g. /Users/Details
and I click on this link it redirects me to /Users/Index
.
How can I get the correct path in the href
regardless of my current position on the site?
This question is related to
asp.net
asp.net-mvc
asp.net-mvc-4
razor
href
Here '~' refers to the root directory ,where Home is controller and Download_Excel_File is actionmethod
<a href="~/Home/Download_Excel_File" />
You can also use this very simplified form:
@Html.ActionLink("Come back to Home", "Index", "Home")
Where :
Come back to Home
is the text that will appear on the page
Index
is the view name
Home
is the controller name
Try the following:
<a asp-controller="Users" asp-action="Index"></a>
(Valid for ASP.NET 5 and MVC 6)
how about
<li>
<a href="@Url.Action("Index", "Users")" class="elements"><span>Clients</span></a>
</li>
You can modify with the following
<li><a href="./Index" class="elements"><span>Clients</span></a></li>
The extra dot means you are in the same controller. If you want change the controller to a different controller then you can write this
<li><a href="../newController/Index" class="elements"><span>Clients</span></a></li>
If using ASP.NET Core, you can adjust the accepted answer to:
<a href="@Url.Action("Index", null, new { area = string.Empty, controller = "User" }, @Context.Request.Scheme)">
<span>Clients</span>
</a>
replacing @Request.Url.Scheme
with @Context.Request.Scheme
Source: Stackoverflow.com