[英]ASP.Net MVC Core 2 - Area Routing
我正在嘗試在我的ASP.Net MVC Core 2應用程序中實現管理員Area
。
我已將區域路由配置如下:
// Default route
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
// Admin area route
app.UseMvc(routes =>
{
routes.MapRoute(
name: "admin",
template: "{area=Admin}/{controller=Home}/{action=Index}/{id?}");
});
這一切都很好。
盡管_ViewStart.cshtml
位於Areas/Admin/Views
目錄中,但該Admin area
使用的Layout
與主要網站相同,但是仍然可以正常使用。
我遇到的問題是導航菜單組件位於主站點布局文件中,並且所有錨點中的href
鏈接位於“管理”區域內時都指向錯誤的URL。
說我有以下鏈接:
<a asp-controller="Account" asp-action="Index">My Account</a>
<a asp-controller="Shopping" asp-action="Basket">Shopping Basket</a>
<a asp-controller="Admin" asp-action="Users">Manage Users</a>
現在,在“管理”區域中時,鏈接是相對於該區域的,因此看起來就像如下所示:
http://localhost/Admin/Account/Index
http://localhost/Admin/Shopping/Basket
http://localhost/Admin/Admin/Users
有沒有什么好方法可以使所有這些鏈接相對於站點根目錄?
如何在應用程序中進行設置有幾個問題。
app.UseMvc()
。 我認為基本上最后一個將覆蓋您的第一個設置。 這就是為什么您看到所有鏈接都帶有/admin
區域前綴的原因。 admin
區域下生成指向用戶管理的鏈接時,應改用asp-area
,例如<a asp-area="admin" asp-controller="users" asp-action="index">Manage Users</a>
。 這就是我要設置區域的方式。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// The area routing setup has to come before your default routing!
// Remember the order of these setups is very important!
// Mvc will use that template as soon as it finds a matching!
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller=dashboard}/{action=index}/{id?}"
);
routes.MapRoute(
name: "default",
template: "{controller=home}/{action=index}/{id?}"
);
}
// Assume you have an Admin area under /Areas/Admin
namespace DL.SO.Web.UI.Areas.Admin.Controllers
{
[Area("admin")]
public abstract class AdminControllerBase : Controller
{
}
}
// Dashboard controller. I know you have home controller inside
// your admin area but they should work the same.
namespace DL.SO.Web.UI.Areas.Admin.Controllers
{
public class DashboardController : AdminControllerBase
{
public IActionResult Index()
{
return View();
}
}
}
// Users controller. I know you have User(s) controller but I usually
// just keep the name of the controller singular.
namespace DL.SO.Web.UI.Areas.Admin.Controllers
{
public class UserController : AdminControllerBase
{
public IActionResult Index()
{
return View();
}
}
}
// My habit is to always specify the area with the anchor tag helper.
// For public links (those are not under any areas):
// I just pass empty string, like asp-area=""
// For links pointing to any controller under any area:
// Just pass the area, like asp-area="admin"
// http://localhost/account
<a asp-area="" asp-controller="account" asp-action="index">My Account</a>
// http://localhost/shopping/basket
<a asp-area="" asp-controller="shopping" asp-action="basket">Shopping Basket</a>
// http://localhost/admin/user
<a asp-area="admin" asp-controller="user" asp-action="index">Manage Users</a>
// http://localhost/admin/dashboard
<a asp-area="admin" asp-controller="dashboard" asp-action="index">Admin Panel</a>
您可以編輯要顯示為首選的URL的模板。 即更改為
template: "{controller=Home}/{action=Index}/{id?}");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.