我有这个:
<li><a href="/Users/Index)" class="elements"><span>Clients</span></a></li>
效果不错。但是如果我已经在这个页面或者控制器上,例如 /Users/Details,我点击这个链接,它会将我重定向到 /Users/Index。
/Users/Details
/Users/Index
我如何才能得到正确的路径在 href不管我当前的位置在网站上?
href
how about
<li> <a href="@Url.Action("Index", "Users")" class="elements"><span>Clients</span></a> </li>
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
http://localhost:10000/Users
I hope this helps.
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.
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>
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 Homeis the controller name
Come back to Home
Index
Home
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
@Request.Url.Scheme
@Context.Request.Scheme
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'); } }) }