5
\$\begingroup\$

Should I leave the Default Route in if it's not adding any benefit?

I have a RouteConfig.cs file that looks like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Dashboard",
        url: "{controller}/{action}",
        defaults: new { controller = "JobDashboard", action = "Index" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "JobDashboard", action = "Index", enumDigit = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "Login",
        url: "{controller}/{action}",
        defaults: new { controller = "Account", action = "Login" }
    );
}

If I only have the Default and Login Routes and the user goes to the root of the site (eg www.sitename.com), then the user always goes to the Login page. I don't want them going to the Login page after they've logged in, and after a little bit of digging I discovered that MVC4 always sorts Route Order Importance by Custom Route first, and then the Default Route.

I created the Dashboard route, and everything is working fine. I then took out the Default route because it didn't seem needed. Nothing seems to be affected by removing the Default Route, and that leads me to the question, should I leave the Default Route in there?

I seem to recall that having a Default Route is good practice, but if it's not adding anything to the solution, is there a good reason to keep it around?

\$\endgroup\$
2
\$\begingroup\$

I am thinking that you would want to keep that default route in there in the case that something happens to the other routes, then there is something to route to if your custom routes get hosed up for some reason.

Defaults are there for a reason, don't get rid of them

\$\endgroup\$
  • \$\begingroup\$ What kind of “something” could happen to the other routes that would leave the default route unaffected? \$\endgroup\$ – svick Nov 29 '13 at 0:45
  • \$\begingroup\$ Errors, Exceptions, someone trying to hack the site by putting things in there that aren't supposed to be there. I am not overly familiar with MVC structure, I just know that Defaults are there for a reason \$\endgroup\$ – Malachi Nov 29 '13 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.