ActionLink和Url。当在Asp.net MVC 4中指定area属性时,操作针对不同的位置

时间:2022-11-30 22:52:19

In _LoggedInUser.cshtml (which is in Views/Shared folder at application's root) view I want to call the Logout method of AC controller. I have two options here:

在_LoggedInUser。cshtml(在应用程序的根视图/共享文件夹中),我想调用AC控制器的注销方法。我有两个选择:

Using ActionLink

使用ActionLink

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

OR

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

I am specifying area because I want to call action method of AC controller irrespective of area it is in.

我指定区域是因为我想调用AC控制器的动作方法,不管它在哪个区域。

As far as I understand the difference between @Html.ActionLink() and @Url.action is that first generates an anchor tag where as second returns a url (Please correct me if I am wrong), so I guess both should target to same location but here @Html.ActionLink has following link location:

就我所知,@Html.ActionLink()和@Url之间的区别。action是第一个生成一个锚标记,第二个返回一个url(如果我错了,请纠正我),所以我猜这两个目标应该指向同一个位置,但是这里是@Html。ActionLink有以下链接位置:

http://localhost:13974/Home/logout?Length=2

whereas <a href="@Url.Action(.... has following link location:

而< a href = " .action(....有链接的位置:

http://localhost:13974/AC/Logout

Both links are working fine when area attribute is removed but @Html.ActionLink() breaks when I specify the area

当删除area属性时,两个链接都可以正常工作,但是当我指定area时,@Html.ActionLink()会中断

Why both the links are targeting to different locations when I specify area?

为什么当我指定区域时,两个链接都指向不同的位置?

3 个解决方案

#1


20  

You can use

您可以使用

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

You can use overload, LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object, Object)

你可以使用重载,LinkExtensions。ActionLink方法(HtmlHelper, String, String, String, String, Object, Object)

For more info visit LinkExtensions.ActionLink

更多信息请访问linkextension . actionlink

Additionally,

此外,

There is no overload as LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)

链接扩展没有重载。ActionLink方法(HtmlHelper, String, String, String, Object)

#2


3  

What you want is this overload:

你想要的是超负荷:

//linkText, actionName, controllerName, routeValues, htmlAttributes

//linkText, actionName, controllerName, routeValues, htmlAttributes

<%=Html.ActionLink("Logout", "Logout", "AC", new {area = string.Empty}, null) %>

try and let us know if this resolves the issue.

试着让我们知道这是否能解决问题。

#3


1  

You are using the wrong overload of the ActionLink method. Change it to

您正在使用ActionLink方法的错误重载。将其更改为

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

#1


20  

You can use

您可以使用

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

You can use overload, LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object, Object)

你可以使用重载,LinkExtensions。ActionLink方法(HtmlHelper, String, String, String, String, Object, Object)

For more info visit LinkExtensions.ActionLink

更多信息请访问linkextension . actionlink

Additionally,

此外,

There is no overload as LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)

链接扩展没有重载。ActionLink方法(HtmlHelper, String, String, String, Object)

#2


3  

What you want is this overload:

你想要的是超负荷:

//linkText, actionName, controllerName, routeValues, htmlAttributes

//linkText, actionName, controllerName, routeValues, htmlAttributes

<%=Html.ActionLink("Logout", "Logout", "AC", new {area = string.Empty}, null) %>

try and let us know if this resolves the issue.

试着让我们知道这是否能解决问题。

#3


1  

You are using the wrong overload of the ActionLink method. Change it to

您正在使用ActionLink方法的错误重载。将其更改为

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