views:

97

answers:

3
+2  Q: 

Vanity MVC Routes?

I want to have a route that looks something like: www.abc.com/companyName/Controller/Action/Id However, all the company names need to map to the same "base" controllers, regardles of what the name is. I only need the companyName for authentication purposes.

Also, if there's no companyName provided, I need to map to a different set of controllers altogether.

How do I do this? I'd also appreciate a good routing resource so I don't have to ask questions like this.

+4  A: 
routes.MapRoute(
    "CompanyRoute",                                        
    "{companyName}/{controller}/{action}/{id}",           
    new { controller = "MyBaseCompanyController", action = "Index", id = "" }  
);

routes.MapRoute(
    "NoCompanyRoute",
    "{controller}/{action}/{id}",
    new {controller = "DifferentDefaultController", action = "Index", id = "" });

Routing is quite a complex topic, but it's covered well in Professional ASP.Net MVC 1.0. For online resources, I would suggest starting here, and then coming back to Stack Overflow ;)

womp
That's what I thought too. However, whenever I try that, I get a "resource not found" error. I don't know why it's not matching my route. Any ideas?
Esteban Araya
Try adding a default value for companyName: new { companyName = "", controller = "MyBaseCompanyController" ...}
Lance Fisher
Hmm... I'd probably have to see your route mapping code and your controller class. You're sure your default action method matches your default controller class?
womp
@Womp: Never mind. The problem was elsewhere in my route the entire time. It was nice getting confirmation from you on what I was doing... that way I was able to look for the problem elsewhere. Thanks!
Esteban Araya
A: 

Go to Global.asax.cs, and add the following route in the RegisterRoutes() method before the "Default" route:

routes.MapRoute(
    "Vanity",                                              // Route name
    "{company}/{controller}/{action}/{id}",                           // URL with parameters
    new { company = "", controller = "Home", action = "Index", id = "" }  // Parameter defaults
);
Lance Fisher
+2  A: 

In case if you wish to Resolve the errors caused due to routing . i suggest the following tool , which i found to be extremely useful.

Route Debugger

vijaysylvester