Separate display and logic
Keep HTML in .cshtml and C# handler logic in .cshtml.cs. This makes larger projects easier to understand.
Pages/Greeting.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace PicnicRazor.Pages;
public class GreetingModel : PageModel
{
public string Message { get; private set; } = "";
public void OnGet() => Message = "Hello from C#!";
}Pages/Greeting.cshtml
@page
@model PicnicRazor.Pages.GreetingModel
<h1>Greeting</h1>
<p>@Model.Message</p>Open /Greeting. Practice: change the greeting and add another property.