MVC区域路由配置问题

MVC area routing configuration issue

我有一个具有以下结构的解决方案

-Controllers
---HomeController
---SomeGeneralController

-Areas
---Admin
-----Controllers
-------AController
-------BController

---Settings
-----Controllers
-------CController
-------DController

我想设置路由,使其遵循以下规则:

/anystringdoesntmatter/somegeneral/1
/lksjflkjs/somegeneral/index/1
/admin/somegeneral/index/1
/settings/somegeneral/index/1    // all these paths should route to to root 'somegeneralcontroller'

所以基本上,当任何 url 有 'somegeneral' 的控制器时,即使有区域匹配,它仍然会路由到根 'somegeneralcontroller'

我知道路由注册顺序很重要,因为提供的是第一个正确的匹配项。考虑到这一点,我的路由设置如下 global.asax.cs

AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);

//RouteConfig.cs

routes.MapRoute(
            name: "Default",
            url: "{path}/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, path = UrlParameter.Optional });


//AreaRegistration.cs

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

任何帮助将不胜感激,因为我有点卡住了。非常感谢

您可以在任何路线注册之前添加 "primary" 路线,例如,

RouteTable.Routes.MapRoute(
            name: "SomeGeneral",
            url: "{path}/SomeGeneral/{action}/{id}",
            defaults: new { controller = "SomeGeneral", action = "Index", id = UrlParameter.Optional});

//Then add the rest of the routes

AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);

第一条路线应该拦截以下网址:

/lksjflkjs/somegeneral/index/1
/admin/somegeneral/index/1
/settings/somegeneral/index/1 

希望这对您有所帮助