In my Asp.NET MVC4 web project, I have many controllers and I want to put them into subdirectories under the folder "Controllers". Some controllers will have the same name due to their major jobs, but they will be put under different subdirectories, which means different namespaces. I want to do a url routing for those controllers. How can I make the routing consider the namespaces that the controllers with the same name exist? Could you show some code how to do that in Global.asax file?
Thanks a lot.
Add a comment
|
2 Answers
You need several MapRoute-s - at lease one for each namespace.
// match all controllers in first namespace
routes.MapRoute(
name: "Namespace1Routes",
url: "path1/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "My.Namespace1" }
);
// match all controllers in second and third namespaces
routes.MapRoute(
name: "Namespace2Routes",
url: "path2/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "My.Namespace2", "My.Namespace3" }
);