What are Razor Pages?
Razor Pages is a page‑focused framework for building web UI in ASP.NET Core. It simplifies the development model by co‑locating the page’s view and its logic, allowing you to write clean, maintainable code with minimal ceremony.
Get StartedGetting Started
Create a new Razor Pages project using the .NET CLI:
dotnet new webapp -o MyRazorApp
Run the app:
cd MyRazorApp
dotnet run
The default template includes a Pages folder with Index.cshtml and its
accompanying Index.cshtml.cs page model.
Project Structure
MyRazorApp/
│
├─ Pages/
│ ├─ Index.cshtml
│ ├─ Index.cshtml.cs
│ ├─ About.cshtml
│ └─ About.cshtml.cs
│
├─ wwwroot/
│ └─ css/
│ └─ site.css
│
├─ Program.cs
└─ MyRazorApp.csproj
Sample Page Model
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace MyRazorApp.Pages
{
public class ContactModel : PageModel
{
public string Message { get; set; } = "Reach us at support@example.com";
public void OnGet()
{
// Initialization logic here
}
public void OnPostSend(string name, string email, string comment)
{
// Process form submission
Message = $"Thank you, {name}! We will reply to {email} shortly.";
}
}
}
Corresponding view (Contact.cshtml):
@page
@model MyRazorApp.Pages.ContactModel
@{
ViewData["Title"] = "Contact";
}
@ViewData["Title"]
@Model.Message
<form method="post">
<label>Name:<input name="name" required /></label><br/>
<label>Email:<input type="email" name="email" required /></label><br/>
<label>Comment:<textarea name="comment" rows="4"></textarea></label><br/>
<button type="submit">Send</button>
</form>
Live Samples
Explore the interactive demo below. Edit the markup and see the result instantly.