c# - 在 Asp.net MVC 4 中指定区域属性时,ActionLink 和 Url.Action 定位不同的位置

标签 c# asp.net asp.net-mvc

在 _LoggedInUser.cshtml(位于应用程序根目录的 Views/Shared 文件夹中) View 中,我想调用 AC Controller 的注销方法。我在这里有两个选择:

使用 Action 链接

@Html.ActionLink("Logout", "Logout", "AC", new { area = string.Empty })

<a href="@Url.Action("Logout", "AC", new { area = string.Empty })">Logout</a>

我指定区域是因为我想调用 AC Controller 的操作方法,而不管它在哪个区域。

据我了解@Html.ActionLink() 和@Url.action 之间的区别在于,首先生成一个 anchor 标记,然后第二个返回一个 url(如果我错了,请纠正我),所以我想两者都应该定位到同一位置,但此处 @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 方法(HtmlHelper、String、String、String、Object)没有重载

关于c# - 在 Asp.net MVC 4 中指定区域属性时,ActionLink 和 Url.Action 定位不同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739206/

相关文章:

c# - 无法加载类型 'WebApplication1.SiteMaster'

javascript - 如何在asp.net c#中的服务器端获取XMLHTTPRequest对象

c# - 试图知道当前在 @Html.DropDownListFor 上选择了哪个元素

c# - PostAsJsonAsync 似乎没有发布正文参数

c# - RedirectToAction 在特定操作(进程)中不起作用

c# - 如何在c#中从Eventhub接收最新消息

c# - 从 Silverlight 调用 WCF 服务

c# - 如何解析多部分 HttpWebResponse

asp.net-mvc - 发送电子邮件属于应用程序的表示层还是业务层?

c# - 不同的 RoutePrefix,相同的 Controller 名称