ASP.NET Core MVC Interview Question #8

How do you create a basic MVC application in ASP.NET Core?

MVC Fundamentals, Controllers, Views & Routing Junior Beginner

Quick Interview Answer

A basic MVC application registers MVC services with AddControllersWithViews, configures the middleware pipeline, and maps one or more controller routes. Controllers are placed in the Controllers folder by convention, while Razor views are typically organized under Views/{ControllerName}.

Detailed Explanation

A basic MVC application registers MVC services with AddControllersWithViews, configures the middleware pipeline, and maps one or more controller routes. Controllers are placed in the Controllers folder by convention, while Razor views are typically organized under Views/{ControllerName}.

The ASP.NET Core project template can create this structure automatically, but understanding the service registration and endpoint mapping is important in interviews and production troubleshooting.

Code Example

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();