Html.ActionLink()中的问题是您不能在其生成的标记内添加其他html内容. 例如,如果您想在文本之外添加图标,例如:
The problem in Html.ActionLink() is that you can't add additional html content inside the tag that it generates. For example, if you want to add an icon besides the text like:
<a href="/Admin/Users"><i class="fa fa-users"></i> Go to Users</a>使用Html.ActionLink(),您只能生成:
Using Html.ActionLink(), you can only generate:
<a href="/Admin/Users">Go to Users</a>因此,要解决此问题,您可以使用Url.Action()仅在标记内生成URL,例如:
So, to resolve this, you can use Url.Action() to generate only the URL inside the tag like:
// 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()通过区域?
So, how do you pass the Area using Url.Action()?
推荐答案您可以使用此Url.Action("actionName", "controllerName", new { Area = "areaName" });
也不要忘记添加控制器的名称空间,以避免管理区域控制器名称和站点控制器名称之间的冲突.
Also don't forget to add the namespace of the controller to avoid a conflict between the admin area controller names and the site controller names.
类似这样的东西
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" } ); }更多推荐
如何通过Url.Action中的Area?
发布评论