views:

39

answers:

1

I think this is a pretty common scenario, but I haven't been able to phrase my problem in such a way that I have generated any useful search engine results. I'd like the Index action of my HomeController to return the action from a different controller.

I have come up with a few solutions but have dismissed each for various reasons:

  • Using a RedirectToAction doesn't seem like the right solution as RedirecToAction results in an HTTP 303 which for a site's homepage seems wrong.
  • Instantiating the other controller and calling its action from within the HomeController Index action; this option just smells bad.
  • Placing the same dependencies as the other controller on the HomeController and utilizing the same repository methods. While this is the easiest of the options I've come up with it seems like a cop out.

So, what am I missing here? Does the ASP.NET MVC framework provide a "right" way to do this?

+3  A: 

How about using the routing engine for that?

routes.MapRoute(
"Special", 
"Home/Index/{id}", 
new { controller = "OtherController", action = "OtherAction", 
id = UrlParameter.Optional }
);

Remember to put more specific routes before less specific ones.

Regards.

uvita
I am sure this has to do w/ a pattern matching problem: your solution works when explicitly accessing `Home/Index` but doesn't work when just accessing the site's route. Is there a pattern I need to add for that scenario as well?
ahsteele
@ahsteele I have no means to test it right now, but I´m sure routing is the key for your problem. Try with only "/" (instead of "Home/Index/{id}"). Remember that the order of the routes is important!
uvita
Using a `"/"` causes an `ArgumentException` but just using `""` works great. Thank you!
ahsteele