Examples

.NET Razor Page App

Building a Razor Page App

.NET Razor Page app creates page-based web UIs.

Introduction to .NET Razor Pages

.NET Razor Pages is a feature of ASP.NET Core that makes coding page-focused scenarios easier and more productive. Razor Pages provide an organized, page-based approach to building web applications, making it an ideal choice for developers who prefer a clean separation of concerns.

Setting Up a Razor Pages Project

To get started with Razor Pages, you need to set up a new ASP.NET Core project using the Razor Pages template. Make sure you have the .NET SDK installed on your machine. You can create a new project using the following command:

Understanding the Folder Structure

The Razor Pages project includes several important folders and files:

  • Pages: Contains Razor pages (.cshtml files) and their corresponding PageModel files (.cshtml.cs).
  • wwwroot: For static files like CSS, JavaScript, and images.
  • Program.cs: Configures the application startup.

Creating a Simple Razor Page

Let's create a basic Razor Page. Navigate to the Pages folder and create a new file named Greeting.cshtml. Add the following content:

Next, create a PageModel class named Greeting.cshtml.cs with the following code:

Running Your Razor Page App

To run the application, navigate to the project directory and use the following command:

Open your browser and go to http://localhost:5000/Greeting to see your Razor Page in action. You should see "Hello, World!" displayed on the page.

Conclusion

Razor Pages in .NET offer a streamlined way to create web applications with a focus on page-based architecture. With its easy setup and clear structure, it's a powerful tool for developers looking to build scalable web applications.