Zapnologica Zapnologica - 10 months ago 102
ASP.NET (C#) Question

ASP MVC href to a controller/view

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.
and I click on this link it redirects me to

How can I get the correct path in the
regardless of my current position on the site?

Answer Source

There are a couple of ways that you can accomplish this. You can do the following:

     @Html.ActionLink("Clients", "Index", "User", new { @class = "elements" }, null)

or this:

     <a href="@Url.Action("Index", "Users")" class="elements">

Lately I do the following:

<a href="@Url.Action("Index", null, new { area = string.Empty, controller = "User" }, Request.Url.Scheme)">

The result would have http://localhost/10000 (or with whatever port you are using) to be appended to the URL structure like:


I hope this helps.