Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 Routing With No Specific Controller

I have a really interesting case:

I have a site where this is already working

  • http://mysite.com/
  • http://mysite.com/browse/mens
  • http://mysite.com/product/1/cooltshirt

But I'd also like these urls to work as well

  • http://mysite.com/farm
  • http://mysite.com/farm/browse/mens
  • http://mysite.com/farm/product/1/cooltshirt

the farm part is fake, it doesn't do anything except to exist for SEO, there is no corresponding controller or action associated with it

Essentially, I have a ton of routes already, but I'd also like the site to behave exactly the same if "farm" is appended after the "mysite" the hostname. So it's kind of like a subdirectory behaving exactly like going on the main site.

like image 429
Max Alexander Avatar asked Jan 24 '26 11:01

Max Alexander


2 Answers

You can add a route with constraint. Make sure it's above the default route.

routes.MapRoute(
    "Farm", // Route name
    "farm/{controller}/{action}", // URL with parameters
    new { controller = "Home", action = "Index"} // Parameter defaults
);

It just makes sure URLs starting with "farm" gets their controller name from 2nd segment and action name from the 3rd segment.

like image 56
Ufuk Hacıoğulları Avatar answered Jan 26 '26 04:01

Ufuk Hacıoğulları


I assume that you have a FarmController, you can just have them rout to your farmcontroller

routes.MapRoute(
    "Farm", // Route name
    "Farm/{controller}/{action}", // URL with parameters
    new { controller = "Farm", action = "Index"} // Parameter defaults
);
like image 42
Jeff Robert Dagala Avatar answered Jan 26 '26 04:01

Jeff Robert Dagala