Tech Junkie Blog: ASP.NET MVC: Change Admin URL to More User Friendly URL With Routing

Monday, September 17, 2018

ASP.NET MVC: Change Admin URL to More User Friendly URL With Routing

In the previous post we created a Admin Area, however we had to type in localhost/Admin/Home/Index to get to the Admin index page.  In this post we are going to make the Index page more user friendly.

To do that first let's assign the namespace to a local variable so that we can use it in multiple routes.

Open the "AdminAreaRegistration.cs" file in the /Areas/Admin folder and change the RegisterArea method to the following:


        public override void RegisterArea(AreaRegistrationContext context) 
        {
            var namespaces = new string[] { typeof(NorthwindCafe.Areas.Admin.Controllers.HomeController).Namespace };

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


Now we are ready to create our custom route so that the user can just type /Admin/
Change the code to the following in RegisterArea method


        public override void RegisterArea(AreaRegistrationContext context) 
        {
            var namespaces = new string[] { typeof(NorthwindCafe.Areas.Admin.Controllers.HomeController).Namespace };

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

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

We created a custom route by using the MapRoute method in the AreaRegistrationContext object.  There are many method overloads in the MapRoute method.  But all we need is the method with the following method signature,  (name, url, object, namespaces).  The first parameter is the name of the route, this could be blank, the second parameter is the actual route, the {id} is optional so I decided to leave that there.  The third parameter is the definition of the object that defines the controller and action to use, and that the id is an optional parameter.


It is important to note that more narrow or descriptive route comes first, before the catchall route which is "Admin/{controller}/{action}/{id}.  MVC will route to the first route that it hits so if you have the more general route firs the more custom route will never be hit because it was already caught by the more general route.





3 comments:

  1. Amazing Article ! I have bookmarked this article page as i received good information from this. All the best for the upcoming articles. I will be waiting for your new articles. Thank You ! Kindly Visit Us @ Coimbatore Travels | Ooty Travels | Coimbatore Airport Taxi

    ReplyDelete
  2. Here is 3d sound subscribe please
    https://www.youtube.com/channel/UCu6CDkK1lz4C7vFmTloPhhg

    ReplyDelete
  3. Nice blog..! I really loved reading through this article. Thanks for sharing such an amazing post with us and keep blogging...Well written article Thank You for Sharing with Us pmp training in chennai | pmp training institute in chennai | pmp training in chennai | pmp training class in chennai | pmp training near me | pmp training courses online

    ReplyDelete