MVC and Razor Pages

Razor Pages are built into ASP.NET Core MVC and use the same features for routing, model binding, filters, authorization, etc.

Comparison:

TechnologyOrganizationRoutingRequest Handling
MVCSeparate folders for Controllers, Models, Views, etc.Attribute-based routingController Actions
Razor PagesA single folder (/Pages)Route based relative to location in folderHandlers

In Visual Studio:

Project templateUsesMissingUse for
Web APIMVC controllers/Views, /Pages — can be addedWeb APIs
Web AppRazor Pages/Views, /Pages — can be addedWeb apps

Razor Pages

The default approach for new web apps in Visual Studio. Razor Pages encapsulates server-side logic for a given logical “page” in a web application.

A Razor Page’s page model combines responsibilities of an MVC controller and a viewmodel. Instead of handling requests with controller actions, page model handlers like OnGet() are executed and render their associated page.

Mapping Requests to Responses

Concepts:

  • Conventional routes (applied in middleware pipeline)
  • Attribute routes (applied to controllers and actions)
  • Content negotiation allows requests to specify how responses should be formatted (JSON, XML, etc)

When a request is matched to a route, but before the action is called, ASP.NET Core MVC will perform model binding and model validation on the request.

  • Model binding converts incoming HTTP data into .NET types specified as parameters of the action method to be called.
    • If the action method expects an int id, model binding will look for values in the route itself, query strings, or posted forms to attempt to provide it.
  • Model validation uses optional attributes on the model type to ensure the model object conforms to certain data requirements.

Preventing Large Controllers

The mediator design pattern reduces coupling between classes while allowing communication between them. Use the MediatR Nuget package.