Reputation: 1077
short question:
Is there a way to configure multiple default controllers in a route??
Long explanation:
I am creating something similar to a portable area in MVC 5. Specifically, I want to create a kind of default controller that will be fired from my portable component if the user does not create it in his application. To be more precise:
_framework
folder, and a DLL gets referenced and the MVC application starts inheriting from my FrameworkHTTPApplication
._framework
folder, and the default routes.HomeController
: If they don't create it, the framework will call FrameworkHomeController
(inside the framework DLL), but if they do create one, it gets served on hitting ~/ . I was able to create the default controller in the framework, and hook it up correctly, but it will not fall back. This is what routeDebugger is showing me:
As you see I have both routes configured (same URL, different defaults). What I am expecting would be: if controller Home
does not exist, then FrameworkHome
should be called.
I have played with the namespaces, but the result is the same.
Any clue on how to do this?
Upvotes: 1
Views: 791
Reputation: 34992
You could solve this using a single route and prioritizing the controllers in the application namespace over the controllers in the framework namespace.
Let´s say the application controllers are defined in the namespace MyApplication.Controllers
and the framework controllers are defined in the namespace Framework.Controllers
.
For the root url /
you want the application to match the controller:
MyApplication.Controllers.HomeController
if defined
Otherwise the controller Framework.Controllers.HomeController
(note this is also named HomeController and not FrameworkHomeController, however it is defined in the framework namespace)
This is achieved with a single default route:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyApplication.Controllers" }
);
Tha namespaces defined in the route are given priority when multiple matching controllers are found. So for the root url /
the default controller name Home
is used, and if the system finds 2 controllers MyApplication.Controllers.HomeController
and Framework.Controllers.HomeController
then the one in the namespace MyApplication.Controllers
will be used.
Upvotes: 1