Tech Junkie Blog - Real World Tutorials, Happy Coding!: Hour 18 ASP.NET Core: Add Area For Administrators

Monday, April 25, 2022

Hour 18 ASP.NET Core: Add Area For Administrators

An ASP.NET MVC can get big, and it could be overwhelming.  Areas are a way to break up the application into smaller segments,  A perfect candidate for an Area is the Administrative features of the site because it has multiple pages, and functionalities.  So it is a good idea to segment off the Administration area to its own area (no pun intended).

To add an Area to your MVC application right-click on the project and click "Add", then select "Area..."

Type in the word "Admin" in the "Add Area" dialog

Make sure AreaRegistration,RegisterAllAreas method is called in the Global.asax.cs file so that your "Admin" area is registered

The Areas folder structure should look like this.

Now we want to create a controller for the Admin Area, right-click "Controllers" folder under "Admin" and click "Add", then select "Controller"

Select and empty controller

Call the controller "HomeController", then click "Add"

Open the HomeController.cs file under the "Contollers" folder in the "Admin" folder, right-click on the "Index" method, then click "Add View"

Except the default options for the view and select "Add"

Open the Index.cshtml and type in the following code

    ViewBag.Title = "Index";


<h1>This is from the Admin Area</h1>

Now if you type in the route /Admin/Home/Index you see the message "This is from the Admin Area"

Now comes the annoying part about Areas, when you type in the route /Home/Index you will get an error that the Home controller is not unique.  That is because the MVC framework puts all controllers in finds in the application into a flat list regardless of namespace.

In order for us to fix this issue we need to specify the namespace in both the "AdminAreaRegistration.cs" file and RouteConfig.cs file

First let's fix the AdminAreaRegistration.cs file, open the file and type in the following code in the RegisterArea method

        public override void RegisterArea(AreaRegistrationContext context) 

                new { action = "Index", id = UrlParameter.Optional },
                new string[] { typeof(NorthwindCafe.Areas.Admin.Controllers.HomeController).Namespace }

Notice in the last line new string[] { "NorthwindCafe.Areas.Admin.Controllers" } we set the namespace for the Admin Area controls so that MVC looks for the controllers that are only in that namespace

Now let's do the same thing for the RouteConfig.cs file. Open the RouteConfig.cs file and change the routes.MapRoute method to the following

                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] {typeof(NorthwindCafe.Controllers.HomeController).Namespace}

Now when you type in the route /Admin/Home/Index you see the Admin Index.cshtml page

And when you type in the /Home/Index route you see the application home page


  1. nice suggestions

  2. useful information

  3. Good and interesting. For any mobile-related information visit us at best seo service

  4. Pg Slot เครดิตฟรี หรือ มีกิจกรรมที่แจกเครดิตฟรี PG SLOT มีโปรโมชั่นและก็ยังมีแอดมินทำงานที่จะรอเอาใจใส่ดูแลผู้เล่นตลอด 24ชั่วโมง ฝากไม่มีอย่างต่ำที่เว็บของเรา Pg-Slot.Game

  5. This comment has been removed by the author.

  6. I’m really impressed with your article.

  7. pg slot 168galaxy ยอดเยี่ยมที่เกมที่มีระบบระเบียบทำการระดับประเทศแบบอย่างเกมน่าเล่นเกมแนวหน้าที่สมัยบริการ24 ช.ค่ายเกม pg slot มาแรงและเป็นค่ายเกมที่มีผู้เล่นเข้าเล่นเยอะ

  8. เล่น pgslot ให้ได้กำไร คือคำตอบที่คุณต้องการ ทำไมถึงเชื่อมั่นว่า PG SLOT พีจีน่าสนใจและเป็นที่นิยมของผู้เล่นสล็อต? เพราะว่า PG มีระบบการเล่นที่ง่าย และสะดวกสบายสำหรับผู้เล่นทุกระดับ

  9. Thank you for sharing this informative and well-written blog post. Your insights are not only thought-provoking but also a great source of certification


Search This Blog