liveBook · Manning

Mvc framework

As explained in Pro ASP.NET Core 7, Tenth Edition

A web application framework based on the Model–View–Controller architectural pattern, in which application concerns are separated into models, views, and controllers.

The MVC framework is an ASP.NET Core application framework built around the Model–View–Controller pattern. It separates application concerns into models, views, and controllers while keeping HTTP requests and HTML visible to the developer. It began as an alternative to Web Forms, whose desktop-style development model could produce unwieldy web projects. In ASP.NET Core, MVC runs on an open, extensible, cross-platform foundation. section 1.1.1 section 19.4

For HTML responses, a controller can define multiple action methods and choose a view while processing a request. A view can also use private or shared partial views. That flexibility is useful for complex features, but not every page needs it: Razor Pages tie markup and C# code together for a more focused approach. The two approaches can coexist. section 23.2

Listing 5.2 Enabling MVC in the Program.cs file in the LanguageFeatures folder
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();

//app.MapGet("/", () => "Hello World!");
app.MapDefaultControllerRoute();

app.Run();

A basic Program.cs setup registers controller-and-view services with AddControllersWithViews(), then maps a default controller route with MapDefaultControllerRoute(). Both statements are needed to turn a minimal web-project configuration into this MVC setup. section 5.1.2

MVC controllers can also provide RESTful web services without using views. For that setup, register services with AddControllers() and map controller routes with MapControllers(). Keeping related operations in a controller avoids repeating response-handling steps across individual endpoints in Program.cs. section 19.4 section 19.4.1

MVC is useful, not compulsory. Single-page applications have reduced the need to organize every web application around server-rendered MVC views, although MVC can still support them through web services. Razor Pages offer less initial preparation for focused features; MVC remains a flexible choice when those features become more complex. section 1.1.1 section 23.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Shows basic Program.cs registration and routing, then contrasts controller-based services with individual endpoints; detailed configuration is deferred.
Examples
Uses Program.cs listings for controller-and-view and web-service setups, alongside a controller-versus-endpoints maintenance comparison.
What it assumes
Assumes a minimal ASP.NET Core web project, while supplying the additional Program.cs statements needed to enable MVC.
What you can do after
Operate/configure it by registering MVC services and mapping routes for either controller-and-view applications or controller-based web services.
How it connects
Connects MVC to the Model–View–Controller pattern, HTTP, Razor views, Razor Pages, RESTful web services, and single-page applications.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.