简体   繁体   English

MVC区域路由配置问题

[英]MVC area routing configuration issue

I have a solution set up with the following structure 我有一个具有以下结构的解决方案

-Controllers
---HomeController
---SomeGeneralController

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

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

I would like to set the routing so it follows these rules: 我想设置路由,使其遵循以下规则:

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

So basically, when any url has a controller of 'somegeneral', even if there is an area match, it will still route to the root 'somegeneralcontroller' 因此,基本上,当任何网址的控制器为“ somegeneralcontroller”时,即使存在区域匹配,它仍将路由至根目录“ somegeneralcontroller”

I am aware that route registration order matters as the first correct match is the one that is supplied. 我知道路线注册顺序很重要,因为第一个正确的匹配是所提供的。 With that in mind, my routing is set as follows in global.asax.cs 考虑到这一点,我的路由在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 }
        );
    }

Any assistance would be very appreciated as I'm a bit stuck. 任何帮助将不胜感激,因为我有点卡住了。 Many thanks 非常感谢

You can add a "primary" route before any route registration like, 您可以在任何路线注册之前添加“主要”路线,例如,

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

the first route should intercept the following urls: 第一条路线应拦截以下网址:

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

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM