Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Getting Started with Razor Page in Asp.Net Core

Getting Started with Razor Page in Asp.Net Core

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

Free ASP.NET Core 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
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this