27
DecGetting Started with Razor Page in Asp.Net Core
Razor Pages in ASP.NET Core
ASP.NET Razor Pages is a server-side, page-focused framework that enables building dynamic, data-driven websites with a clean separation of concerns. It supports cross-platform development and can be deployed to Windows, Unix, and Mac operating systems. It is the recommended framework for cross-platform server-side HTML generation.
In this .NET core tutorial, we will explore how to createRazor Pagesusing Visual Studio. Also, consider learning the ASP.NET Core Courseto better understand the .Net concepts.
What are Razor Pages?
- Razor pages are nothing but a simple and page-focused framework.
- The Razor Page is used to create cross-platform, data-driven, server-side web pages.
Getting Started
1. Start Visual Studio
- First, Open Visual Studio.
- Select New Project.
- Then select ASP.NET Core Web App which is Razor Pages.
- Click Next and In the Configure your new project dialog box, enter MyFirstRazorPages for the Project name.
- It's really important to name the project MyFirstRazorPages, including matching the capitalization.
- Then Select Next.
- After that, In the Additional information dialog:
- Now, Select .NET 8.0 for Long Term Support.
- Verify that not to use top-level statements if unchecked.
- Finally, Select the Create option.
- You can see now the following starter project has been created:
2. Run the application
- Select MyFirstRazorPages in Solution Explorer.
- Then press Ctrl+F5 button to run the application without debugging it.
- The Visual Studio shows the following popup window.
- If the project is not yet configured to use SSL:
- Select Yes if you trust the IIS Express SSL certificate from the above dialog box.
- Select Yes if you agree to trust the given below development certificate.
3. Examine the project files
Pages folder
- This pages folder Contains Razor pages and supporting files.
- Each Razor page is a pair of files they are as follows
- A .cshtml file which has HTML markup with C# using Razor syntax.
- A .cshtml.cs file that has C# code which handles page events.
4. wwwroot folder
- It contains static assets, such as HTML, JavaScript, and CSS files.
5. appsettings.json
It contains configuration data, such as connection strings.
6. Program.cs
It contains the following code, Let's see in C# Compiler:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
7. WebApplicationBuilder
- The following lines of code in this file create a WebApplicationBuilder with preconfigured defaults.
- And add Razor Pages support to the Dependency Injection (DI) container,
- Finally, we build the app:
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); var app = builder.Build();