HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

MVC4 Routes, using Default

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
defaultroutesusingmvc4

Problem

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?

Solution

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

Context

StackExchange Code Review Q#36236, answer score: 2

Revisions (0)

No revisions yet.