Tech Junkie Blog - Real World Tutorials, Happy Coding!: 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.
























Similar Posts:

10 comments:

  1. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    ExcelR Data Analytics courses

    ReplyDelete
  2. This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

    ReplyDelete
  3. Great writing! You have a flair for informational writing. Your content has impressed me beyond words. I have a lot of admiration for your writing. Thank you for all your valuable input on this topic.

    SEO services in kolkata
    Best SEO services in kolkata
    SEO company in kolkata
    Best SEO company in kolkata
    Top SEO company in kolkata
    Top SEO services in kolkata
    SEO services in India
    SEO copmany in India

    ReplyDelete
  4. There are many things mentioned in this article I would not have thought of on my own. This material is inspirational, interesting and it allows the readers to open up their minds to original thinking.
    SAP training in Kolkata
    Best SAP training in Kolkata
    SAP training institute in Kolkata

    ReplyDelete
  5. There are many things mentioned in this article I would not have thought of on my own. This material is inspirational, interesting.
    SAP training in Kolkata
    Best SAP training in Kolkata
    SAP training institute in Kolkata

    ReplyDelete
  6. If I had to give a prime example of great quality content, this article would be one. It's well-written material that keeps your interest well.
    SAP training in Mumbai
    Best SAP training in Mumbai
    SAP training institute Mumbai

    ReplyDelete
  7. That article sounds to me progressively about group joint effort are expel the interchanges hindrance with a solid arrangement and usage, a debt of gratitude is in order for sharing this.


    SEO services in kolkata
    Best SEO services in kolkata
    SEO company in kolkata

    ReplyDelete
  8. Its as if you had a great grasp on the subject matter, but you forgot to include your readers. Perhaps you should think about this from more than one angle.
    data science course in Hyderabad

    ReplyDelete

Search This Blog