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:

16 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. 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
  3. 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
  4. 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
  5. 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
  6. Wonderful blog. I delighted in perusing your articles. This is genuinely an incredible perused for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome!
    data analytics courses in hyderabad with placements

    ReplyDelete
  7. Inventateq in Bangalore offers you the opportunity to learn digital marketing and get hired at a MNC, mid-sized company, or e-commerce company.
    During digital marketing courses in Bangalore, students learn the strategies, tactics, and methods used by digital marketers around the world.
    digital marketing course in Bangalore

    ReplyDelete
  8. Inventateq's digital marketing courses will prepare you to work in the industry. Starting salaries will be below average, but will rapidly increase as you gain experience.
    digital marketing training online

    ReplyDelete
  9. Really awesome blog, useful information. If you want to become a data scientist, here is the best option for you. For more information click the below link.
    Data Science Course in Hyderabad

    ReplyDelete
  10. By reading this article, many things have piled up in my mind which made me to think about it. You have put some high quality and valuable information here that any reader would love to read. I sincerely share many of your views in this article.
    SAP training in Kolkata
    SAP course in Kolkata

    ReplyDelete
  11. เสริมดวง สีเสื้อ มงคล ห้ามพลาด นักเสี่ยงโชค Pg มาเปลี่ยนแปลงชีวิตให้ดียิ่งขึ้นดังเช่นว่า เสริมดวงการคลังด้วยกระเป๋าเงินใหม่ แก้ปีชงให้สุขใจ pg และก็ที่ต้องมีให้ได้เลยเป็น เสริมดวงด้วยเสื้อ

    ReplyDelete
  12. พักสายตา netflix มาเล่น เกม pg กันเถอะ เว็บเกมสล็อตที่กำลังมาแรงมากๆ ในตอนนี้ ไม่แพ้เน็ตฟลิกซ์เลย ก็คือ เว็บเกม pg-slot.game นั่นเอง pgslot มีเกมสล็อตต่างๆมากมายกว่า100เกม

    ReplyDelete
  13. เล่นสล็อตออนไลน์ ผ่านเว็บไซต์ PG SLOT ในช่วงเวลานี้ที่การเล่นสล็อตออนไลน์จัดว่าเป็นที่นิยมเป็นอันมาก เพราะว่าผู้คนจำนวนมากรับประกันว่า ได้เงินจริง pgslot บางบุคคลมีโชคจัดหนักทำแจ็คพอตแตก

    ReplyDelete
  14. PGSlot 444 นั้น คุณสามารถเข้าเล่นได้โดยตรงผ่านเว็บไซต์ของพวกเขา โดยที่คุณจะต้องทำการสมัครสมาชิกก่อนเพื่อเข้าใช้งานบริการต่างๆ PG SLOT ที่มีให้ในเว็บไซต์ ธรรมชาติ

    ReplyDelete

Search This Blog