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

Wednesday, August 8, 2018

ASP.NET MVC: 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";
}

<h2>Index</h2>

<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) 
        {

            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                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


            routes.MapRoute(
                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



















No comments:

Post a Comment