[英]ActionLink and Url.Action target different locations when area attribute is specified in Asp.net MVC 4
在_LoggedInUser.cshtml(位於應用程序根目錄下的Views / Shared文件夾中)視圖中,我想調用AC控制器的Logout方法。 我有兩個選擇:
使用ActionLink
@Html.ActionLink("Logout", "Logout", "AC", new { area = string.Empty })
要么
<a href="@Url.Action("Logout", "AC", new { area = string.Empty })">Logout</a>
我指定區域因為我想調用AC控制器的動作方法而不管它在哪個區域。
據我所知,@ Html.ActionLink()和@ Url.action之間的區別是首先生成一個錨標記,其中第二個返回一個網址(如果我錯了請糾正我),所以我猜兩個都應該針對相同位置但在這里@ Html.ActionLink有以下鏈接位置:
http://localhost:13974/Home/logout?Length=2
而<a href="@Url.Action(
....有以下鏈接位置:
http://localhost:13974/AC/Logout
當刪除區域屬性時,兩個鏈接都正常工作,但是當我指定區域時,@ Html.ActionLink()會中斷
當我指定區域時,為什么兩個鏈接都定位到不同的位置?
您可以使用
@Html.ActionLink("Logout", "Logout", "AC", new { area = string.Empty }, null)
你可以使用重載, LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object, Object)
有關更多信息,請訪問LinkExtensions.ActionLink
另外,
LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)
沒有重載LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)
你想要的是這個重載:
// linkText,actionName,controllerName,routeValues,htmlAttributes
<%=Html.ActionLink("Logout", "Logout", "AC", new {area = string.Empty}, null) %>
如果這解決了問題,請嘗試告訴我們。
您正在使用ActionLink
方法的錯誤重載。 將其更改為
@Html.ActionLink("Logout", "Logout", "AC", null, new { area = string.Empty })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.