Use Area in ASP.NET Core Razor Pages

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

Select Empty Template

Click Ok button to Finish




Open Startup.cs file and add new configurations as below:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace LearnASPNETCoreRazorPagesWithRealApps
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddRazorPagesOptions(options =>
            {
                options.AllowAreas = true;
            });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            app.UseMvc();
        }
    }
}

Select Views folder and right click to select Add\New Item Menu

Select Web\ASP.NET in left side. Select Razor View Imports item and click Add button to Finish

In _ViewImports.cshtml file and TagHelpers library as below:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers




In project, create new folder named Areas. In Areas folder, create new folder named SuperAdmin. In SuperAdmin folder, create new folder named Pages. In Pages folder, create new Razor Page named Index as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Areas.SuperAdmin.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Areas.SuperAdmin.Pages.IndexModel

<!DOCTYPE html>

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

    <h3>Super Admin Panel</h3>

</body>
</html>

In Areas folder, create new folder named Admin. In Admin folder, create new folder named Pages. In Pages folder, create new Razor Pages as below:

In Pages folder, create new Razor Page named Index as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Areas.Admin.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Areas.Admin.Pages.IndexModel

<!DOCTYPE html>

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

    <h3>Work 1 - Admin Panel</h3>

</body>
</html>




In Pages folder, create new Razor Page named Index2 as below:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Areas.Admin.Pages
{
    public class Index2Model : PageModel
    {
        [BindProperty(Name = "id", SupportsGet = true)]
        public string Id { get; set; }

        public string Msg { get; set; }

        public void OnGet()
        {
        }

        public void OnGetWork2()
        {
            Msg = "Selected Id: " + Id;
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Areas.Admin.Pages.Index2Model

<!DOCTYPE html>

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

    <h3>Work 2 - Admin Panel</h3>
    @Model.Msg

</body>
</html>

In Pages folder, create new Razor Page named Index3 as below:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Areas.Admin.Pages
{
    public class Index3Model : PageModel
    {
        [BindProperty(Name = "id1", SupportsGet = true)]
        public string Id1 { get; set; }

        [BindProperty(Name = "id2", SupportsGet = true)]
        public int Id2 { get; set; }

        public string Msg { get; set; }

        public void OnGet()
        {
        }

        public void OnGetWork2()
        {
            Msg = "Id1: " + Id1;
            Msg += "<br>Id2: " + Id2;
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Areas.Admin.Pages.Index3Model

<!DOCTYPE html>

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

    <h3>Work 3 - Admin Panel</h3>
    @Html.Raw(Model.Msg)

</body>
</html>




Create new folder named Pages. In Pages folder, create new Razor Page named Index as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.IndexModel
@{
    Layout = null;
}

<!DOCTYPE html>

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

    <ul>
        <li>
            <a asp-area="SuperAdmin" asp-page="index">SuperAdmin Panel</a>
        </li>
        <li>
            Admin Panel
            <ul>
                <li>
                    <a asp-area="Admin" asp-page="index" asp-page-handler="work1">Work 1</a>
                </li>
                <li>
                    <a asp-area="Admin" asp-page="index2" asp-page-handler="work2" asp-route-id="p01">Work 2</a>
                </li>
                <li>
                    <a asp-area="Admin" asp-page="index3" asp-page-handler="work2" asp-route-id1="p01" asp-route-id2="123">Work 3</a>
                </li>
            </ul>
        </li>
    </ul>

</body>
</html>




Open Index Razor Page with following url: http://localhost:1115

Click SuperAdmin Panel link to open Index Razor Page in SuperAdmin Area with following url: http://localhost:1115/SuperAdmin/Index

Click Work 1 link to open Index Razor Page in Admin Area with following url: http://localhost:1115/Admin/Index?handler=work1

Click Work 2 link to open Index2 Razor Page in Admin Area with following url: http://localhost:1115/Admin/Index2?id=p01&handler=work2

Click Work 3 link to open Index3 Razor Page in Admin Area with following url: http://localhost:1115/Admin/Index3?id1=p01&id2=123&handler=work2