ASP.Net MVC核心2 – 区域路由
我正在尝试在我的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?}"); });
一切都很好。
此管理area
使用与主网站相同的Layout
,尽管_ViewStart.cshtml
位于Areas/Admin/Views
目录中,但这仍然可以正常工作。
我遇到的问题是导航菜单组件存在于主站点布局文件中,并且当在Admin区域内时,所有锚点中的href
链接指向错误的URL。
说我有以下链接:
My Account Shopping Basket Manage Users
在管理区域内,链接现在相对于该区域,因此看起来如下所示:
http://localhost/Admin/Account/Index http://localhost/Admin/Shopping/Basket http://localhost/Admin/Admin/Users
有没有什么好方法可以使所有这些链接相对于站点根目录?
如何在应用程序中进行设置有几个问题。
- 你不能两次使用
app.UseMvc()
。 我认为基本上最后一个将覆盖你的第一个设置。 这就是为什么你看到所有链接都有/admin
区域前缀。 - 如果要在
admin
区域下生成用户管理的链接,则应使用asp-area
,例如Manage Users
。
这就是我设置区域的方式。
设置区域路由以及启动中的默认路由
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?}" ); }
使用[Area]注释设置管理基础控制器,这样您就不必在管理区域下的所有其他控制器中指定该控制器。
// 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 My Account // http://localhost/shopping/basket Shopping Basket // http://localhost/admin/user Manage Users // http://localhost/admin/dashboard Admin Panel
您可以编辑要显示为首选URL的模板。 即改为
template: "{controller=Home}/{action=Index}/{id?}");
- ASP.Net Core 2.1中的身份:自定义AccountController
- hosting.json有什么意义,因为appsettings.json就足够了
- ASP.Net Core 2配置占用了大量内存。 如何以不同方式获取配置信息?
- ASP.NET Core 2.0中缺少声明转换支持
- 如何让JWT在Autorest生成的SDK中工作? (ASP.NET Core 2.0)
- 设置JsonConvert.DefaultSettings asp net core 2.0不能按预期工作
- 使用多个提交建模和validation表单
- ASP.NET Core 2.0将Cookie和承载授权结合在一起,用于同一端点
- .net-core-2.0 azure app service 502.5错误