ASP.Net MVC Core 2

编程入门 行业动态 更新时间:2024-10-26 01:33:38
本文介绍了ASP.Net MVC Core 2-区域路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在ASP.Net MVC Core 2应用程序中为管理员实现Area.

I'm trying to implement an Area for Administrators within my ASP.Net MVC Core 2 application.

我已将该区域的路线配置如下:

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?}"); });

这一切都很好.

此管理员area使用与主网站相同的Layout,尽管_ViewStart.cshtml位于Areas/Admin/Views目录中,但这仍然可以正常工作.

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.

我遇到的问题是导航菜单组件位于主站点布局文件中,并且所有锚点中的href链接位于管理区域内时指向错误的URL.

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.

说我有以下链接:

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

localhost/Admin/Account/Index localhost/Admin/Shopping/Basket 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().我认为基本上最后一个将覆盖您的第一个设置.这就是为什么您看到所有链接都具有/admin区域前缀的原因.
  • 如果要在admin区域下生成指向用户管理的链接,则应改用asp-area,例如<a asp-area="admin" asp-controller="users" asp-action="index">Manage Users</a>.
  • You can't use app.UseMvc() twice. I think basically the last one will override your first setup. That's why you are seeing all links have /admin area prefix.
  • When you want to generate a link to your user management under admin area, you should use asp-area instead, like <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?}" ); }

    设置带有[Area]批注的admin基本控制器,这样您就不必在所有其他应位于admin区域内的控制器中指定该控制器.

    // 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" // localhost/account <a asp-area="" asp-controller="account" asp-action="index">My Account</a> // localhost/shopping/basket <a asp-area="" asp-controller="shopping" asp-action="basket">Shopping Basket</a> // localhost/admin/user <a asp-area="admin" asp-controller="user" asp-action="index">Manage Users</a> // localhost/admin/dashboard <a asp-area="admin" asp-controller="dashboard" asp-action="index">Admin Panel</a>

    更多推荐

    ASP.Net MVC Core 2

    本文发布于:2023-11-14 13:28:43,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1587340.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Net   ASP   Core   MVC

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!