MVC vanity routes?

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

Also, if no company is listed, I need to map a different set of controllers altogether.

How should I do it? I also appreciate a good routing resource, so I don't need to ask questions like this.

+2


source to share


3 answers


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 a fairly complex topic, but it is well reflected in Professional ASP.Net MVC 1.0 . For online resources, I suggest starting here and then going back to Stack Overflow;)

+4


source


If you want to resolve the errors caused by routing. I suggest the following tool which I find extremely useful.



Route Debugger

+1


source


Go to Global.asax.cs and add the following route to 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
);

      

0


source







All Articles