Html.ActionLink() 中的问题是您无法在它生成的标记内添加其他 html 内容。 例如,如果您想在文本之外添加一个图标,例如:
<a href="/Admin/Users"><i class="fa fa-users"></i> Go to Users</a>
使用Html.ActionLink(),只能生成:
<a href="/Admin/Users">Go to Users</a>
因此,要解决此问题,您可以使用 Url.Action() 仅生成标记内的 URL,如下所示:
// Here, Url.Action could not generate the URL "/admin/users". So this doesn't work.
<a href="@Url.Action("", "Users", "Admin")"><i class="fa fa-usesr"></i> Go to Users</a>
// This works, as we know it but won't pass the Area needed.
<a href="@Url.Action("", "Users")"><i class="fa fa-users"></i> Go to Users</a>
那么,如何使用 Url.Action() 传递区域?
最佳答案
您可以使用此Url.Action("actionName", "controllerName", new { Area = "areaName"});
另外,不要忘记添加 Controller 的命名空间,以避免管理区域 Controller 名称与站点 Controller 名称之间发生冲突。
类似这样的事情
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "Site.Mvc.Areas.Admin.Controllers" }
);
}
关于asp.net-mvc - 如何在Url.Action中传递Area?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341268/