[英]ASP.Net MVC Core 2 - Area Routing
I'm trying to implement an Area
for Administrators within my ASP.Net MVC Core 2 application. 我正在尝试在我的ASP.Net MVC Core 2应用程序中实现管理员
Area
。
I have configured the route for the area as follows: 我已将区域路由配置如下:
// 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?}");
});
This all works pretty well. 这一切都很好。
This Admin area
uses the same Layout
as the main website albeit that the _ViewStart.cshtml
lives in the Areas/Admin/Views
directory but this still works fine. 尽管
_ViewStart.cshtml
位于Areas/Admin/Views
目录中,但该Admin area
使用的Layout
与主要网站相同,但是仍然可以正常使用。
The issue I'm having is with a navigation menu component which lives in the main site layout file and the href
links in all anchors pointing to the wrong URL when inside the Admin area. 我遇到的问题是导航菜单组件位于主站点布局文件中,并且所有锚点中的
href
链接位于“管理”区域内时都指向错误的URL。
Say I have the following links: 说我有以下链接:
<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>
When inside the Admin area, the links are now relative to the area and thus appear as if they were as follows: 现在,在“管理”区域中时,链接是相对于该区域的,因此看起来就像如下所示:
http://localhost/Admin/Account/Index
http://localhost/Admin/Shopping/Basket
http://localhost/Admin/Admin/Users
Is there any nice way to make all of these links relative to the site root? 有没有什么好方法可以使所有这些链接相对于站点根目录?
There are couple issues how you set things up in your application. 如何在应用程序中进行设置有几个问题。
app.UseMvc()
twice. app.UseMvc()
。 I think basically the last one will override your first setup. /admin
area prefix. /admin
区域前缀的原因。 admin
area, you should use asp-area
instead, like <a asp-area="admin" asp-controller="users" asp-action="index">Manage Users</a>
. admin
区域下生成指向用户管理的链接时,应改用asp-area
,例如<a asp-area="admin" asp-controller="users" asp-action="index">Manage Users</a>
。 This is how I will setup areas. 这就是我要设置区域的方式。
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>
You can edit the template for the URL to be displayed as preferred. 您可以编辑要显示为首选的URL的模板。 ie change to
即更改为
template: "{controller=Home}/{action=Index}/{id?}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.