Area in ASP.NET MVC


On the Visual Studio, create new ASP.NET MVC Web Application project

Select Empty Template and Core Reference is MVC




In Controllers folder, create new controller named HomeController.cs as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LearnASPNETMVCWithRealApps.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

In Views/Home folder, create new razor view named Index.cshtml as below:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>

    <h3>Index View</h3>

</body>
</html>

Open RouteConfig.css file and add new namespace as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace LearnASPNETMVCWithRealApps
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                // Namespace for Website
                namespaces: new[] { "LearnASPNETMVCWithRealApps.Controllers" }
            );


        }
    }
}




In project, create new areas as below:

In this area, create new Controller, Views and Namespace as below:

In Controllers folder, create new controller named HomeController.cs as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LearnASPNETMVCWithRealApps.Areas.Admin.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

In Views/Home folder, create new razor view named Index.cshtml as below:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>

    <h3>Admin Area</h3>
    Index View

</body>
</html>

Open AdminAreaRegistration.css file and add new namespace as below:

using System.Web.Mvc;

namespace LearnASPNETMVCWithRealApps.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                // Namespace for Admin Area
                new[] { "LearnASPNETMVCWithRealApps.Areas.Admin.Controllers" }
            );
        }
    }
}

In this area, create new Controller, Views and Namespace as below:

In Controllers folder, create new controller named HomeController.cs as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LearnASPNETMVCWithRealApps.Areas.User.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

In Views/Home folder, create new view named Index.cshtml as below:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>

    <h3>User Area</h3>
    Index View

</body>
</html>

Open UserAreaRegistration.css file and add new namespace as below:

using System.Web.Mvc;

namespace LearnASPNETMVCWithRealApps.Areas.User
{
    public class UserAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "User";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "User_default",
                "User/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                // Namespace for User Area
                new[] { "LearnASPNETMVCWithRealApps.Areas.User.Controllers" }
            );
        }
    }
}




Access Index action in Home controller with following url: http://localhost:49328/Home/Index

Output

Index View

Access Index action in Home controller in Admin area with following url: http://localhost:49328/Admin/Home/Index

Output

Access Index action in Home controller in User area with following url: http://localhost:49328/User/Home/Index

Output

I recommend you refer to the books below to learn more about the knowledge in this article: