ASP.NET Core MVC Interview Question #9

What does AddControllersWithViews do?

MVC Fundamentals, Controllers, Views & Routing Junior Beginner

Quick Interview Answer

AddControllersWithViews registers services required for controllers with views. It enables the MVC controller infrastructure together with view-related services needed for server-rendered applications.

Detailed Explanation

AddControllersWithViews registers services required for controllers with views. It enables the MVC controller infrastructure together with view-related services needed for server-rendered applications.

It is appropriate for a traditional ASP.NET Core MVC application. AddControllers is more focused on controller/API scenarios, while AddRazorPages registers Razor Pages services. An application can register more than one of these when its architecture requires it.

Code Example

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();