Create ASP.NET MVC Project
On the Visual Studio, create new ASP.NET MVC Web Application project
Select Empty Template and Core Reference is MVC
Create Controller
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();
}
}
}
Create 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>
Declare Namespace
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" }
);
}
}
}
Create Areas
In project, create new areas as below:
Admin Area
In this area, create new Controller, Views and Namespace as below:
Home Controller
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();
}
}
}
Index 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>
Declare Namespace
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" }
);
}
}
}
User Area
In this area, create new Controller, Views and Namespace as below:
Home Controller
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();
}
}
}
Index 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>
Declare Namespace
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" }
);
}
}
}
Structure of ASP.NET MVC Project
Run Application
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
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- ASP.NET MVC Framework Unleashed
- Programming Microsoft ASP.NET MVC (3rd Edition) (Developer Reference)
- Pro ASP.NET MVC 5 Platform
- Pro ASP.NET MVC Framework
- Professional ASP.NET MVC 5