Model-View-Controller (MVC)
Model-View-Controller (MVC) is a design pattern that separates an application into model, view, and controller: data and business logic, presentation, and input handling. ASP.NET Core MVC implements it for web applications.
Model-View-Controller (MVC) is an architectural pattern that separates an application into three groups of components: the model (state and business logic), the view (presentation), and the controller (accepts input and chooses what comes back as the answer). The goal is the separation of concerns.
The three roles in ASP.NET Core
Microsoft's documentation describes the flow like this: a request is routed to a controller. It works with the model to carry out the user's action or fetch the result of a query, chooses the view, and hands it the model data it needs.
- Model: represents the state of the application and the business logic. Views often get ViewModel types of their own that contain only what the page shows.
- View: an HTML template with embedded Razor markup, in ASP.NET Core a
.cshtmlfile. It presents and contains as little logic as possible. - Controller: a class whose methods ("actions") handle requests. It is meant to check input and choose the answer, but not to contain business logic or data access; it delegates to services and the domain model.
The direction of the dependency matters: view and controller depend on the model, but the model depends on neither view nor controller. That way, the business logic can be built and tested without a user interface.
public sealed class ContactsController(IContactService contacts) : Controller
{
public async Task<IActionResult> Details(int id)
{
Contact? contact = await contacts.FindAsync(id);
if (contact is null)
{
return NotFound();
}
return View(contact);
}
}
MVC and the other ways in ASP.NET Core
MVC is only one of several ways to build web user interfaces and interfaces in ASP.NET Core. Razor Pages put the page and its handler side by side in one folder. Minimal APIs describe endpoints directly. Blazor builds the user interface from components written in C# and HTML. The web API template uses the same controllers as MVC, just without views. You can mix the approaches in one project.
For a new application, MVC is not a must. The pattern, the separation of presentation, input, and business logic, stays useful without controllers too.
An example from the Kladde
In the course, the Kladde, the sample application of a small IT office, uses no MVC controllers: its API consists of minimal API endpoints, and its pages are Blazor components. You still find the idea of the pattern, because the rules live in a core that the API and the user interface share, and the pages only present.
Common misconceptions
- MVC is not a framework, but a pattern. ASP.NET Core MVC is a framework that implements it.
- A fat controller is not MVC. When rules sit in the controller, the separation is gone.
- MVC is not the same as frontend and backend. With MVC, all three roles run on the server.
In the course
The academy does not teach MVC as a topic of its own but relies on minimal APIs and Blazor. You learn the web API in K2, module 2.3, in The first web API, and the user interface in module 2.5 in The Blazor Web App. Why rules live in the model and not at the edge is shown in Validation at the edge, rules in the model.
Further reading on the blog
Sources
- Microsoft Learn, Overview of ASP.NET Core MVC: https://learn.microsoft.com/aspnet/core/mvc/overview?view=aspnetcore-10.0
- Microsoft Learn, Develop ASP.NET Core MVC apps: https://learn.microsoft.com/dotnet/architecture/modern-web-apps-azure/develop-asp-net-core-mvc-apps