【问题标题】:ASP.Net MVC Core 2 - Area RoutingASP.Net MVC Core 2 - 区域路由
【发布时间】:2018-05-16 16:22:50
【问题描述】:

我正在尝试在我的 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 目录中,但这仍然可以正常工作。

我遇到的问题是导航菜单组件位于主站点布局文件中,并且所有锚点中的 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

有没有什么好的方法可以让所有这些链接都相对于站点根目录?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core-2.0


    【解决方案1】:

    如何在应用程序中进行设置存在一些问题。

    1. 您不能使用app.UseMvc() 两次。我认为基本上最后一个会覆盖你的第一个设置。这就是为什么您会看到所有链接都有 /admin 区域前缀。
    2. 当您想在admin 区域下生成指向您的用户管理的链接时,您应该改用asp-area,例如&lt;a asp-area="admin" asp-controller="users" asp-action="index"&gt;Manage Users&lt;/a&gt;

    这就是我将如何设置区域。

    设置区域路由以及启动中的默认路由

    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
    <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>
    

    【讨论】:

    • 谢谢您-您的评论非常有帮助。你是对的,我不能多次使用app.useMvc,但是以下方法确实有效:app.UseMvc(routes =&gt; { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); routes.MapRoute( name: "admin", template: "{area=Admin}/{controller=Home}/{action=Index}/{id?}"); });
    • 保持紧张。我正在向您展示如何正确地做到这一点!
    • 同样,设置路由的顺序很重要!您可以阅读我在上面的编码示例中放入的 cmets。
    • 您的答案是完美的,完全符合预期。我一直在实施它,因为你一直在更新帖子,但它真的很好用。谢谢你,非常感谢!
    【解决方案2】:

    您可以编辑要显示为首选 URL 的模板。即更改为

        template: "{controller=Home}/{action=Index}/{id?}");
    

    【讨论】:

    • 我已更新我的问题以包含默认路线。这在Configure 方法中已经存在
    • 不是admin区要改路由吗?
    • 我是否需要将您的模板建议添加为管理区域内的路由配置?
    猜你喜欢
    • 2011-03-17
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2020-09-01
    • 2018-06-30
    • 2010-12-09
    • 2018-05-31
    • 1970-01-01
    相关资源
    最近更新 更多