ASP.NET Core MVC Beginner

Project structure and Program.cs

A new MVC project contains a handful of folders and files, and each has a clear job. This lesson tours the project, then reads Program.cs line by line so you understand how the app is configured and in which order things run.

How Program.cs runs, top to bottom
  1. StartCreateBuilderLoads configuration, logging and the hosting environment
  2. Step 2Register servicesbuilder.Services.AddControllersWithViews() and your own services
  3. Step 3Buildbuilder.Build() creates the app with all registered services
  4. Step 4Add middlewareError handling, HTTPS redirection, routing, authorization
  5. Step 5Map endpointsMapControllerRoute connects URLs to controller actions
  6. EndRunapp.Run() starts the server and waits for requests

Services are registered before Build; middleware and endpoints are added after it.

The folders and files

Most of your time is spent in Controllers, Models and Views. The rest is configuration you change less often.

  • Controllers: classes that handle requests, such as HomeController.
  • Models: classes for your data and view models.
  • Views: Razor .cshtml files, one subfolder per controller, plus Shared for layouts and partials.
  • wwwroot: static files such as CSS, JavaScript, images and libraries.
  • appsettings.json and appsettings.Development.json: configuration values like connection strings and log levels.
  • Properties/launchSettings.json: local run profiles, ports and environment for development only.
  • MyFirstApp.csproj: the target framework and NuGet package references.

Program.cs line by line

Program.cs is the entry point. It uses top-level statements, so there is no Main method to write. The template looks roughly like this (details vary a little between .NET versions).

C#
var builder = WebApplication.CreateBuilder(args);

// 1. Services: things the app can ask for through dependency injection
builder.Services.AddControllersWithViews();

var app = builder.Build();

// 2. Middleware: runs for every request, in this order
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();

// 3. Endpoints: which code handles which URL
app.MapStaticAssets();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}")
    .WithStaticAssets();

app.Run();

In Visual Studio 2026

Solution Explorer (View, Solution Explorer, or Ctrl+Alt+L) shows the same folders. A few things there are easy to miss.

  • Dependencies lists the .NET framework and the NuGet packages. Right-click the project and choose Manage NuGet Packages to add more.
  • appsettings.Development.json is nested under appsettings.json; click the small arrow to see it.
  • Properties holds launchSettings.json. Right-click the project, Properties, then Debug, Open debug launch profiles UI to change ports or the environment.
  • Double-click the project name to open the .csproj file and see the target framework, such as net10.0.

Common mistakes

Most startup bugs come from putting a line in the wrong part of Program.cs.

  • Registering a service after builder.Build(). The service collection is read-only by then.
  • Changing the order of middleware without a reason. Order decides what runs first.
  • Putting secrets such as passwords in appsettings.json that is committed to source control. Use user secrets or environment variables.
  • Editing launchSettings.json and expecting production to change. It is only used when running locally.

Interview question

What is the difference between builder.Services and app.Use... calls in Program.cs?

builder.Services registers services in the dependency injection container before the app is built, for example AddControllersWithViews or your own repositories. The app.Use... calls run after Build and add middleware to the request pipeline, which executes for every request in the order it was added. Services describe what is available; middleware describes what happens to each request.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.