Moving from Pages to Views

In this lesson, we will describe the differences between pages and views and how to reorganize a project by using controllers and Razor views instead of Razor pages.

Razor pages versus Razor views

We already know that Razor pages have their own URL and retrieve all data they need to build the HTTP response, while Razor views are fed data by controllers. More specifically, controllers pass an object instance to the Razor views they invoke.

The @page directive declares that a Razor file is a page, so views have no @page directive at the beginning of the file. Instead, they declare the type of object they can receive from a controller. This way, one may access it in a type-safe way from inside the view code.

The object passed to a view is called a model or a ViewModel, so it should not be a surprise that the model type is declared with a @model directive:

Get hands-on with 1200+ tech skills courses.