User987
User987

Reputation: 3823

Defining multiple static routes and 1 dynamic route for 1 controller in .NET MVC

I have defined a couple of static routes which look like following:

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

And third is the dynamic route which looks like following:

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

I would like to have defined these two static routes for my Index controller:

/Index/Tutorials
/Index/LogOut

Every other route should point to:

/Index/{id}

The way I defined it right now works for 2 static routes, but when I try to pass parameter like this which isn't one of the two static routes like following:

http://localhost:60617/Index/12345/

Where 12345 is ID, I get following error:

The resource cannot be found.

How can I define these routes properly ? Can someone help me out ?

Here is the route class:

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


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

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


    }
}

Upvotes: 1

Views: 954

Answers (1)

Nkosi
Nkosi

Reputation: 247068

The order in which you map routes are important.

Generic routes should be mapped after more specific routes to avoid route conflicts.

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

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

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

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

        routes.MapRoute(
          name: "ResetPwdRoute",
          url: "User/ResetPwd/{id}",
          defaults: new { controller = "User", action = "ResetPwd", id = UrlParameter.Optional }
        );

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

Upvotes: 1

Related Questions