views:

122

answers:

4

Is there a way to create a base controller implementation that handles all the routes?

IE /home/index and /about/index all point to one controller method and that returns the view.

The site I am building is 90% static content and I dont want to go and create 50 controllers.

One should be fine?

+1  A: 

Remember that "controller" and "action" are keywords for the routing system to do it's dynamic magic. If you just replace the "controller" parameter in your route with some other parameter name, you can always use a default controller.

routes.MapRoute("Default",
                "{whatever}/{action}/{id}",
                new { controller="Home", action="Index", whatever="", id="0" } );

Note that each method should handle the "whatever" parameter.

womp
Thing is routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Base", action = "Index", id = "" } // Parameter defaults );Those all hit the BaseController but routes like "/site-faq/banking-faq dont get hit
john
Can't you just define more routes to catch them? /{whatever}/{action}. /{whatever}/{action}/stuff/{morestuff*}. Remember you should put your routes from most specific to least specific.
womp
routes.MapRoute( "Leaderboard", "site-promotions/site-leader-board", new { controller = "Base", action = "Index" } );.but when I hit /site-promotions/site-leader-board it never hits the base controller's index method
john
The route tester works? and says it will hit the base conroller but its not
john
A: 

ya with proper routing in your global.asax file you can do this. But it will be difficult for you to manage website after some time for refactoring or any reason. Here is the to learn routing in asp .net mvc: http://www.asp.net/learn/mvc/#MVC_Routing

deepesh khatri
A: 

Since you are not going to map to controllers or methods based on the URL, you just need to capture a couple of parameters, and then do something with them.

 routes.MapRoute( 
     "MyNewRoute", 
     "{firstParameter}/{secondParameter}",
     new {controller="Home", action="Index"} 
 ); 

 public ActionResult Index(string firstParameter, string SecondParameter)
 {
     if (firstParameter == "Home")
     {
        // Do something
     }
 }
Robert Harvey
I think the "-" are breaking the route
john
A: 

I understand your desire to keep things simple, but in my experience, you always have to expect some level of change down the line. If I were in your situation, I would do something like this:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Index", new { controller = "Main" });
    }
}

public class AboutController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Index", new { controller = "Main" });
    }
}

public class MainController : Controller
{
    public ActionResult Index()
    {
        // Do something important.
        // Do something else important.
        return View();
    }
}

To create the AboutController, I simply copied the HomeController and changed the class name to AboutController. This way, you keep the route maintenance effort to a minimum, you centralize the behaviors you want to centralize, and you still allow yourself the ability to adapt to changing requirements without having to undo a lot of workarounds. If, after a period of time, you have to change the behavior of a particular action, you just go to that action and change the code.

Neil T.