105

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?

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
Zapnologica
  • 22,170
  • 44
  • 158
  • 253

8 Answers8

196

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

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

or this:

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

Lately I do the following:

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

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

http://localhost:10000/Users
starball
  • 20,030
  • 7
  • 43
  • 238
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
21

how about

<li>
<a href="@Url.Action("Index", "Users")" class="elements"><span>Clients</span></a>
</li>
Valamas
  • 24,169
  • 25
  • 107
  • 177
FosterZ
  • 3,863
  • 6
  • 38
  • 62
12

Try the following:

<a asp-controller="Users" asp-action="Index"></a>

This is part of the new Anchor Tag Helper system for Razor pages that was introduced in ASP.NET Core 1.0.

JdeBP
  • 2,127
  • 16
  • 24
John Schroeder
  • 121
  • 1
  • 2
6

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>
rksajib
  • 180
  • 1
  • 14
6

Here '~' refers to the root directory ,where Home is controller and Download_Excel_File is actionmethod

 <a href="~/Home/Download_Excel_File" />
faux
  • 91
  • 1
  • 10
3

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
Homeis the controller name

Alexandre Neukirchen
  • 2,713
  • 7
  • 26
  • 36
0

If you want to use one modal for Create and Update You can also do this

C#

onclick="showInPopup('@Url.Action("CreateOrUpdate","Request",null,Context.Request.Scheme)','Create Request')"


onclick="showInPopup('@Url.Action("CreateOrUpdate","Request",new{id = item.id },Context.Request.Scheme)','Edit Request')"

JS

showInPopup = (url, title) => {
    $.ajax({
        type: "GET",
        url: url,
        success: function (res) {
            $("#form-modal .modal-body").html(res);
            $("#form-modal .modal-title").html(title);
            $("#form-modal").modal('show');
        }
    })
}
Æthenwulf
  • 213
  • 2
  • 19
-1

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