views:

69

answers:

1

Following on from this question:

ASP.NET MVC Routing with Default Controller

I have a similar requirement where my end user doesn't want to see the controller name in the url for the landing or "home page" for their application.

I have a controller called DeviceController which I want to be the "home page" controller. This controller has a number of actions and I'd like to use URL's like the following:

http://example.com  -> calls Index()  
http://example.com/showdevice/1234 -> calls ShowDevice(int id)  
http://example.com/showhistory/1224 -> calls ShowHistory(int id)  

I also need links generated for this controller to leave out the /device part of the url.

I also have a number of other controllers, for example BuildController:

http://example.com/build  
http://example.com/build/status/1234  
http://example.com/build/restart/1234  

and so on. The URL's for these controllers are fine as they are.

The problem is that I just can't seem to get my head around the routing for this even after studying the answers to the question referenced above.

Can someone provide a code sample explaining how to do this?

I'm using ASP.NET MVC2.

+3  A: 

Try this:

   private void RegisterRoutes(RouteCollection routes) {

      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute("default", "{controller}/{action}/{id}",
         new { action = "index", id = "" },
         // Register below the name of all the other controllers
         new { controller = @"^(account|support)$" });

      routes.MapRoute("home", "{action}",
         new { controller = "device", action = "index" });
   }

e.g. /foo

If foo is not a controller then it's treated as an action of the device controller.

Max Toro
Thanks Max, this does the trick.
Kev