Getting Started with Razor Page in Asp.Net Core

Getting Started with Razor Page in Asp.Net Core

28 May 2024
Advanced
1.71K Views
6 min read
Learn with an interactive course and practical hands-on labs

.NET Course

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.
  •  Start Visual Studio:

  • You can see now the following starter project has been created:

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:

Run the application

  • 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.

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(); 
Conclusion
So in this article, we have learned about Getting Started with Razor Page in Asp.Net Core. I hope you enjoyed learning these concepts while programming with .Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Consider our .NET Certification Training to learn .net from scratch.

FAQs

MVC works good with apps with many dynamic server views, single-page apps, REST APIs, and AJAX calls.On the other hand, Razor Pages are perfect for simple pages that are read-only or do basic data input.

To create a razor page, first create a folder named Pages and then right-click the folder, select the Add option, and select Razor Page to create a new Razor Page. 

It is designed to make common patterns used with web browsers easy to implement when building an app.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core Certification TrainingNov 08MON, TUE, WED, THU, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
ASP.NET Core Certification TrainingNov 10SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this