What is ASP.NET Core MVC?
ASP.NET Core MVC is a framework for building web apps and APIs using the Model-View-Controller pattern. This lesson explains what each of the three parts does, how a request flows through them, and how to switch MVC on in a new app.
- StartBrowser asks for a URLGET /products/details/5
- Step 2ControllerProductsController.Details(5) handles the request
- Step 3ModelThe controller loads the Product data through a service
- Step 4ViewDetails.cshtml turns the Product into HTML
- EndBrowser shows the pageThe finished HTML is sent back
The controller coordinates; the model holds data and rules; the view only displays.
The idea in plain words
MVC stands for Model, View and Controller. It is a way of splitting a web app into three parts so that each part has one clear job. The controller receives the request, the model holds the data and the business rules, and the view turns that data into the HTML the browser shows.
Splitting the work this way keeps code easier to read, test and change. You can redesign a page without touching the business rules, and you can test a controller without starting a web server. ASP.NET Core MVC is Microsoft's implementation of this pattern, and it runs on the same cross-platform runtime as the rest of ASP.NET Core.
- Model: the data and the rules that apply to it, such as a Product class and a check that its price is positive.
- View: a Razor file (.cshtml) that mixes HTML with C# to display the model.
- Controller: a class that handles the request, talks to services, picks a model and chooses which view to return.
How a request flows
When a browser asks for a URL such as /products/details/5, routing looks at the path and picks a controller and an action method, here ProductsController.Details with id set to 5. The action loads the product, usually through a service or a database context, and passes it to a view. The view renders HTML and the response goes back to the browser.
Notice that the view never fetches data on its own and the model knows nothing about HTTP. The controller is the coordinator in the middle. Keeping controllers thin, with real logic in services or the model, is one of the most common pieces of advice for MVC apps.
Example
A new MVC app registers controllers with views in Program.cs and then maps a default route. The route template below means: the first URL segment is the controller name, the second is the action, and an optional third segment is an id. When segments are missing, Home and Index are used.
The controller and view underneath show the full round trip for one page.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
// Controllers/HomeController.cs
public class HomeController : Controller
{
public IActionResult Index()
{
var model = new WelcomeModel { Message = "Hello from MVC" };
return View(model); // renders Views/Home/Index.cshtml
}
}
public class WelcomeModel
{
public string Message { get; set; } = "";
}The view
The view declares the type of its model with the @model directive and then uses Model to read the data. Razor encodes the output for you, so text that comes from users cannot inject script into the page.
@* Views/Home/Index.cshtml *@
@model WelcomeModel
<h1>@Model.Message</h1>
<p>Today is @DateTime.Now.ToString("dddd").</p>Common mistakes
Most beginner problems come from putting code in the wrong layer or from the naming conventions MVC relies on.
- Putting database queries and business rules directly in controllers, which makes them long and hard to test. Move that work into services.
- Naming a controller class without the Controller suffix, or putting a view in the wrong folder, so MVC cannot find it.
- Calling AddControllers instead of AddControllersWithViews in an app that returns views. AddControllers is meant for APIs only.
- Writing logic in views. Views should only display data that the controller already prepared.
Going further
ASP.NET Core also offers Razor Pages, which puts the handler code next to the page instead of in a separate controller, and Blazor for interactive UI in C#. MVC remains a good fit when you want a clear separation between request handling and presentation, or when the same controllers also serve a JSON API. The next lessons look at controllers, routing and views in detail.
Interview question
What are the responsibilities of the model, the view and the controller in ASP.NET Core MVC?
The model represents the app's data and business rules. The view is a Razor template that presents the model as HTML and should contain only display logic. The controller handles the incoming request, works with the model and services, and chooses the result to return, such as a view, a redirect or JSON. This separation makes each part easier to test and change on its own.
Want the full details? Read the official Microsoft documentation for this topic.