Asp.net-Mvc
如何在 Url.Action 中傳遞區域?
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" });另外不要忘記添加控制器的命名空間以避免管理區域控制器名稱和站點控制器名稱之間的衝突。
像這樣的東西
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" } ); }