ASP.NET Core MVC · Foundations

Introduction to ASP.NET Core MVC

ASP.NET Core MVC is Microsoft’s framework for building server-rendered websites in C#. A request comes in, a controller decides what to do, a model carries the data, and a Razor view turns it into HTML. In this track we will build one real app with it: ShopSmart, a small online store.

Beginner 6 min Lesson 1 of 42Examples target .NET 9.
Before you start:C#: Classes and Objects
0/42 in ASP.NET Core MVC
On this page · 6 min · Beginner

In simple words#

MVC is a restaurant: the customer’s order reaches the waiter (Controller), the kitchen prepares the food (Model/data), and the plate is presented nicely (View). Each does one job.

Model
Data and business rules.
View
The HTML page the user sees.
Controller
Handles the request and picks the view.

Learning objectives#

  • Explain what ASP.NET Core MVC is used for
  • Describe the job of the Model, the View and the Controller
  • Follow one request from the browser to the HTML that comes back
  • Read a complete minimal MVC app

Explanation#

ASP.NET Core is the cross-platform, open-source web platform for .NET. It runs on Windows, Linux and macOS, in containers and in the cloud. MVC is one way to build apps on it — the one to choose when the server renders HTML pages (shops, dashboards, admin panels, content sites).

MVC stands for Model–View–Controller. The Controller receives the request and decides what happens. The Model is the data (a product, a list of orders, a form). The View is a Razor template (.cshtml) that turns the model into HTML.

Splitting the work this way keeps each part simple: controllers have no HTML, views have no database code, and models can be tested without a browser.

Conventions do a lot of the wiring for you: a URL like /Products/Details/7 calls the Details action on ProductsController with id = 7, and View() looks for Views/Products/Details.cshtml.

Compared with the old ASP.NET MVC 5 (.NET Framework), ASP.NET Core is faster, cross-platform, has dependency injection and configuration built in, and is where all new features go. Examples in this track target .NET 9.

Syntax#

Text Illustrative
Browser ──GET /──▶ Controller (HomeController.Index)
                      │ builds the Model (StoreSummary)
                      ▼
                   View (Views/Home/Index.cshtml) ──HTML──▶ Browser

Code example: A complete MVC app in three files#

C#Program.cs Illustrative
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();                  // keep the demo output short
builder.Services.AddControllersWithViews();        // MVC = controllers + Razor views


var app = builder.Build();
app.MapDefaultControllerRoute();                   // {controller=Home}/{action=Index}/{id?}


app.Urls.Add("http://127.0.0.1:5101");
await app.StartAsync();


// Act like a browser: ask for the home page and print what comes back
using var http = new HttpClient { BaseAddress = new Uri("http://127.0.0.1:5101") };
var response = await http.GetAsync("/");
Console.WriteLine(
quot;
{(int)response.StatusCode} {response.Content.Headers.ContentType}");
Console.WriteLine((await response.Content.ReadAsStringAsync()).Trim()); await app.StopAsync(); // Models/StoreSummary.cs public record StoreSummary(string StoreName, int ProductCount); // Controllers/HomeController.cs using Microsoft.AspNetCore.Mvc; public class HomeController : Controller { public IActionResult Index() { var model = new StoreSummary("ShopSmart", 3); // the Model return View(model); // the View: Views/Home/Index.cshtml } } @* Views/Home/Index.cshtml *@ @model StoreSummary <h1>Welcome to @Model.StoreName</h1> <p>@Model.ProductCount products in stock</p>
Expected output
200 text/html; charset=utf-8
<h1>Welcome to ShopSmart</h1>
<p>3 products in stock</p>

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. AddControllersWithViews registers everything MVC needs; MapDefaultControllerRoute connects URLs to controllers.
  2. The request for / matches the default route, so ASP.NET Core calls HomeController.Index().
  3. Index creates a StoreSummary — the model — and passes it to View().
  4. View() finds Views/Home/Index.cshtml by convention; @model StoreSummary makes Model strongly typed.
  5. Razor replaces @Model.StoreName and @Model.ProductCount with values, and the HTML is sent back as text/html.

Real-world use case#

ShopSmart’s home page, product pages, cart and admin screens are all controllers and views like this one. Later lessons add a database, forms, validation, login and deployment.

Common mistakes#

Putting HTML in controllers

Building HTML strings in C# makes pages impossible to maintain. Controllers return View(model); views own the markup.

Database code in views

Views should only display the model they receive. Loading data belongs in the controller or a service.

Confusing ASP.NET MVC 5 with ASP.NET Core MVC

Tutorials for MVC 5 use Global.asax, Web.config and System.Web — none of which exist in ASP.NET Core.

Best practices#

  • Keep controllers thin and views simple
  • Use strongly typed models (@model)
  • Follow naming conventions so routing and views just work
  • Target a supported .NET version (LTS for long-lived apps)

Remember this#

Controller decides. Model carries data. View renders HTML.

/Products/Details/7 → ProductsController.Details(7).

View() → Views/{Controller}/{Action}.cshtml.

Practice exercise#

Your task: Add an About action to HomeController that passes the text "Since 2026" to a view Views/Home/About.cshtml, which shows it in an <h2>.

Need a hint?
return View((object)"Since 2026") — the cast picks the View(object model) overload, because View(string) means a view name.
Show solution
C# Illustrative
// Controllers/HomeController.cs
using Microsoft.AspNetCore.Mvc;


public class HomeController : Controller
{
    public IActionResult About() => View((object)"Since 2026");
}


@* Views/Home/About.cshtml *@
@model string
<h2>@Model</h2>

Mini quiz#

Introduction to ASP.NET Core MVC quizMultiple choice Question 1 of 3

In MVC, which part turns data into HTML?

Score so far: 0/0

Want more? Practise all ASP.NET Core MVC MCQs · ASP.NET Core MVC interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.