This blog post describes my oversimplified understanding of the ASP.NET Core razor page component model.
Razor is a syntax for embedding HTML and C# in .cshtml files that run on the server to generate HTML. Significant C# belongs in other files, but is used in views for data retrieval, looping, and otherwise.
The application determines a model class to pass to each razor component (page view, page view, partial view, or view component).
A razor page consists of a PageModel and a Razor Page Template.
- Razor PageModel: The PageModel class or a derivative services the request, generally by implementing the OnGet() method to construct a model to pass to the razor page template.
- Razor Page Template: The razor page template uses razor syntax in a .cshtml file to render data from the PageModel. The .cshtml file contains a @page directive that controls which routes (URL patterns) activate the razor page. By default, this directive requires a separate razor page template for each page serviced by the ASP.NET Core razor pages infrastructure.
- Razor Partial View: Partial views are razor templates that retrieve data from and call methods of the model passed to the partial view to generate a section of the page. Partial views are appropriate for rendering when all data and logic is available in the model.
- Razor View Components: View components are like partial views with an additional class that typically assembles the model for the view template to generate a section of the page. View components are appropriate for constructing more complex models for views.
- Sections: Another feature of ASP.NET Core razor pages that I will not describe here because I prefer alternatives.