Multiple Default Routes in ASP.NET MVC to different Default Actions

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2010-05-26T00:35:02Z Indexed on 2010/05/26 0:41 UTC
Read the original article Hit count: 388

I have multiple controllers with different actions (no "Index" actions). The actions I consider "default" actions, are named differently. I want to create default routes for their names and have the first available action (from my list of default actions) executed if only the controller name is provided in the route.

So, for example, I have the following actions which I want to consider default and want checked for their existence in a controller in this order:

List() 
Draw()
ViewSingle() 

The routing should somehow search for /{controller} and then take the first available action from the list above as default action, e.g.:

/ControllerA     -> ControllerA.List()
/ControllerB     -> ControllerB.Draw()
/ControllerC     -> ControllerC.ViewSingle()
/ControllerD     -> ControllerD.Draw()
/ControllerE     -> ControllerE.List()

Is this possible? I tried creating additional Default actions like this but couldn't get it to work:

routes.MapRoute("Default1", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "List" } 

routes.MapRoute("Default2", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "Draw" } 

routes.MapRoute("Default3", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "ViewSingle" } 

Help?

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc