ASP.NET Core 2具有区域的默认路由 [英] ASP.NET Core 2 default route having areas

查看:328
本文介绍了ASP.NET Core 2具有区域的默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cI处理了有关区域路由的各种帖子,但仍然无法解决我的问题。

cI went through various posts regarding the Areas routing but still I am not able to resolve my issue.

我想通过两种方式拆分应用程序。

I would like to split my application in the way that there are two routes.


  • /区域/面板

  • /区域/网站

  • /Areas/Panel
  • /Areas/Website

在每个区域内都有 HomeController 和对应于操作的适当方法。

Inside each area there is HomeController and appropriate methods that correspond to actions.

我希望每当用户着陆到任何1级路线时,即:

I would like that whenever user lands to any level 1 route i.e.:


  • /主页

  • /联系人

  • /关于/公司

  • 等。

  • /home
  • /contact
  • /about/company
  • etc.

定向到 / Areas / Website / {controller} / {action}


  • / panel

  • / panel / home

  • / panel / users / 2

  • etc

  • /panel
  • /panel/home
  • /panel/users/2
  • etc

/区域/面板/ {controller} / {action}

我当前的MVC路线是:

My current MVC route is:

   app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

            routes.MapRoute(
                name: "area",
                template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
        });

但它不起作用,可能是因为我不完全了解如何告诉路由器使用网站为默认区域。我在控制器本身之前尝试了各种指令,但没有用。

but it isn't working, probably because I don't fully understand how tell the router to use Website area as default. I tried various directives right before the controller itself but it did not work.

请问您的建议吗?

预先谢谢您。

推荐答案

一个不起作用的原因是因为您以错误的顺序注册了路由。从路线表的顶部到底部对路线进行评估,并且第一场比赛获胜。

One reason it doesn't work because you have the routes registered in the wrong order. The routes are evaluated from the top to the bottom of the route table and the first match wins.

另一个问题是,您需要将默认路线设置为区域路由(使用 MapAreaRoute 扩展方法),如果您希望它将请求定向到网站区域。

Another issue is that you need to make the "default" route into an area route (using the MapAreaRoute extension method) if you want it to direct requests to the Website area.

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "area",
        template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

    routes.MapAreaRoute(
        name: "default",
        areaName: "Website",
        template: "{controller=Home}/{action=Index}/{id?}");
});

参考:为什么在asp.net mvc中先映射特殊路由,然后再映射公共路由?

这篇关于ASP.NET Core 2具有区域的默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆