Create ASP.NET Core Razor Pages Project
On the Visual Studio, create new ASP.NET Core Web Application project
Select Empty Template
Click Ok button to Finish
Configurations
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();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvc();
}
}
}
Create Razor View Imports
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
Master Layout Page
Create new folder named Pages. In Pages folder, create new folder named Shared. In Shared folder, create Razor Layout named _MasterLayout.cshtml as below:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<a asp-page="index">Home</a> |
<a asp-page="aboutus">About Us</a> |
<a asp-page="news">News</a>
<br /><br />
@RenderBody()
<br /><br />
Copyright PMK Lab
</body>
</html>
AboutUs Layout Page
In Shared folder, create Razor Layout named _AboutUsLayout.cshtml as below:
@{
ViewData["Title"] = "About Us";
Layout = "~/Pages/Shared/_MasterLayout.cshtml";
}
<a asp-page="about1">About 1</a> |
<a asp-page="about2">About 2</a>
<br /><br />
@RenderBody()
News Layout Page
In Shared folder, create Razor Layout named _NewsLayout.cshtml as below:
@{
ViewData["Title"] = "News";
Layout = "~/Pages/Shared/_MasterLayout.cshtml";
}
<a asp-page="news1">News 1</a> |
<a asp-page="news2">News 2</a> |
<a asp-page="news3">News 3</a>
<br /><br />
@RenderBody()
Index Razor Page
In Pages folder, create new Razor Page named Index as below:
Index.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
public class IndexModel : PageModel
{
public void OnGet()
{
}
}
}
Index.cshtml
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.IndexModel
@{
ViewData["Title"] = "Index";
Layout = "~/Pages/Shared/_MasterLayout.cshtml";
}
<h1>Home Page</h1>
AboutUs Razor Page
In Pages folder, create new Razor Page named AboutUs as below:
AboutUs.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
public class AboutUsModel : PageModel
{
public void OnGet()
{
}
}
}
AboutUs.cshtml
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.AboutUsModel
@{
ViewData["Title"] = "About Us";
Layout = "~/Pages/Shared/_AboutUsLayout.cshtml";
}
<h1>About Us</h1>
About1 Razor Page
In Pages folder, create new Razor Page named About1 as below:
About1.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
public class About1Model : PageModel
{
public void OnGet()
{
}
}
}
About1.cshtml
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.About1Model
@{
ViewData["Title"] = "About 1";
Layout = "~/Pages/Shared/_AboutUsLayout.cshtml";
}
<h1>About 1</h1>
About2 Razor Page
In Pages folder, create new Razor Page named About2 as below:
About2.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
public class About2Model : PageModel
{
public void OnGet()
{
}
}
}
About2.cshtml
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.About2Model
@{
ViewData["Title"] = "About 2";
Layout = "~/Pages/Shared/_AboutUsLayout.cshtml";
}
<h1>About 2</h1>
News Razor Page
In Pages folder, create new Razor Page named News as below:
News.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
public class NewsModel : PageModel
{
public void OnGet()
{
}
}
}
News.cshtml
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.NewsModel
@{
ViewData["Title"] = "News";
Layout = "~/Pages/Shared/_NewsLayout.cshtml";
}
<h1>News</h1>
News1 Razor Page
In Pages folder, create new Razor Page named News1 as below:
News1.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
public class News1Model : PageModel
{
public void OnGet()
{
}
}
}
News1.cshtml
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.News1Model
@{
ViewData["Title"] = "News 1";
Layout = "~/Pages/Shared/_NewsLayout.cshtml";
}
<h1>News 1</h1>
News2 Razor Page
In Pages folder, create new Razor Page named News2 as below:
News2.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
public class News2Model : PageModel
{
public void OnGet()
{
}
}
}
News2.cshtml
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.News2Model
@{
ViewData["Title"] = "News 2";
Layout = "~/Pages/Shared/_NewsLayout.cshtml";
}
<h1>News 2</h1>
News3 Razor Page
In Pages folder, create new Razor Page named News3 as below:
News3.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
public class News3Model : PageModel
{
public void OnGet()
{
}
}
}
News3.cshtml
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.News3Model
@{
ViewData["Title"] = "News 3";
Layout = "~/Pages/Shared/_NewsLayout.cshtml";
}
<h1>News 3</h1>
Structure of ASP.NET Core Razor Pages Project
Run Application
Open Index Razor Page with following url: http://localhost:1115
Open AboutUs Razor Page with following url: http://localhost:1115/AboutUs
Open About1 Razor Page with following url: http://localhost:1115/About1
Open About2 Razor Page with following url: http://localhost:1115/About2
Open News Razor Page with following url: http://localhost:1115/News
Open News1 Razor Page with following url: http://localhost:1115/News1
Open News2 Razor Page with following url: http://localhost:1115/News2
Open News3 Razor Page with following url: http://localhost:1115/News3