ASP MVC href to a controller/view

asp.netasp.net Mvcasp.net Mvc-4RazorHref

asp.net Problem Overview


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?

asp.net Solutions


Solution 1 - asp.net

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

I hope this helps.

Solution 2 - asp.net

how about

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

Solution 3 - asp.net

Try the following:

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

(Valid for ASP.NET 5 and MVC 6)

Solution 4 - asp.net

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

 <a href="~/Home/Download_Excel_File" />

Solution 5 - asp.net

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>

Solution 6 - asp.net

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

Solution 7 - asp.net

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');
        }
    })
}

Solution 8 - asp.net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZapnologicaView Question on Stackoverflow
Solution 1 - asp.netBrendan VogtView Answer on Stackoverflow
Solution 2 - asp.netFosterZView Answer on Stackoverflow
Solution 3 - asp.netJohn SchroederView Answer on Stackoverflow
Solution 4 - asp.netfauxView Answer on Stackoverflow
Solution 5 - asp.netrksajibView Answer on Stackoverflow
Solution 6 - asp.netAlexandre NeukirchenView Answer on Stackoverflow
Solution 7 - asp.netÆthenwulfView Answer on Stackoverflow
Solution 8 - asp.netNeil SchurrerView Answer on Stackoverflow