MVC Route URL to Area controller

124 views Asked by At

Hi I want to route to my AdminLoginController (inside Admin area) with this url siteurl/admin. My code in RegisterArea(AreaRegistrationContext context) function:

    context.MapRoute(
                    "Admin_1", 
                    "Admin", 
                    new { controller = "AdminLogin", action = "Index" }        
                );
    
//    the default route map

    context.MapRoute(
                    "Admin_default",
                    "Admin/{controller}/{action}/{id}",
                    new { controller="AdminLogin", action = "Index", id = UrlParameter.Optional}
                );

However I can only go to my desired page through siteurl/Admin/AdminLogin , not siteurl/Admin.

If I change the custom route as:

context.MapRoute(
                "Admin_1", 
                "", 
                new { controller = "AdminLogin", action = "Index" }        
            );

then I can go to my desired page (AdminLogin Index page) when using url siteurl/.

How can I customize the MapRoute to go to my page with siteurl/Admin ?

1

There are 1 answers

2
S Bradshaw On BEST ANSWER

I tried to replicate this in a blank project with just a home and admin login controller, but it appears that the following route should work as expected:

context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller="AdminLogin", action = "Index", id = UrlParameter.Optional}
        );

The issue you are having may well be due to the order, as if placed after the default route, it fails, as when supplied "siteurl/Admin" it will first match the default route.