In the last module we took a look at the initial files generated by the ASP.Net Core Empty Web project template. Now it’s time to start getting our hands dirty. We need to create a Controller to accept HTTP requests and a View to render data to the user. But first we need to configure the project as an MVC Application. We do that in the Program.cs file we looked at in the last module, the entry point to the application that sets up and runs the application.
Modify Program.cs – Configure the application for MVC
Open the Program.cs file by double clicking it in the Solution Explorer inside of Visual Studio.

Modify its contents with the code shown below.
FredsCars/Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add Services
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
/*** remove initial endpoint ***/
// app.MapGet("/", () => "Hello World!");
/*** Add endpoints for contoller actions and
the default route ***/
app.MapDefaultControllerRoute();
app.Run();
In the code above we just made a few minor modifications.
C# Comments
Remember from Chapter 1 where we learned about HTML and JavaScript comments? Well, C# has comments too. They look like JavaScript comments.
A one line comment starts with two forward slashes like this:
// My one line comment
Multiple line comments start with a forward slash and Asterix like this /* and end with an Asterix and forward slash like this */. The following is an example of a multiline comment in C#.
/* My mulitline comment.
Here is another line.
And one more line.
*/
Adding Services and Creating Endpoints
Notice in particular two comments we added.
/*** ... ***/
// Add Services
/*** ... ***/
// Configure the HTTP request pipeline.
/*** ... ***/
These two comment lines denote the two major sections in Program.cs we use to configure any ASP.Net Core application whether it is an MVC, Razor Pages, or Blazor application or some combination thereof.
- Adding Services
- Configuring the HTTP request pipeline
- Register middleware components
- Create and configure Endpoints.
Add the MVC service
In the Add Services section of Program.cs, we add the services for controllers with the following line.
builder.Services.AddControllersWithViews();
We need to add these services for controllers because the MVC architectural pattern is based on Controllers and Views as opposed to Razor Pages which use Page Models and Views. (We will see Razor Pages in the next chapter)
The AddControllersWithViews
method configures the MVC services for the commonly used features with controllers with views.
So far we have only added this single service because it’s the only service we need to get started and run our project as an MVC application. But we will see many more types of services available that we can use in later modules and chapters. And eventually the concept of services will lead us to a software development pattern called DI (Dependency Injection).
Add the default Controller Route
Also notice we “commented out” the MapGet
endpoint
/*** remove initial endpoint ***/
// app.MapGet("/", () => "Hello World!");
We did this because we no longer want our base URL (https://localhost:40443) to merely return the string “Hello World!”. We want our endpoints to be handled by the controllers we are going to create. So we call the MapDefaultControllerRoute
method of the WebApplication
object to add endpoints for controller actions.
app.MapDefaultControllerRoute();
The MapDefaultControllerRoute
method adds a default route with the following pattern.
{controller=Home}/{action=Index}/{id?}
This route has three segments which match up with an incoming URL request. The first segment specifies the default controller as the Home controller if none is specified. The second segment specifies the default action as the Index action in the controller if no action is specified. The third segment is an optional route value named id and is marked as optional by the ‘?’ character.
With the above route in place, here are some examples of how some URLs would work.
https://localhost:40443/users/add
The above URL would go to a controller called users and an action method with in the controller called add.
https://localhost:40443/users/index/4
The above URL would go to a controller called users and an action method with in the controller called index and the id route value would be 4.
https://localhost:40443
With no controller or action specified, the above URL would go to the default Home controller and default Index action within the controller. No id value would be passed.
Don’t worry if these examples of the default registered route seem fuzzy for now. It will become more clear once we start writing our own controllers.
But I do want to note an important concept of MVC here. Especially MVC in the context of ASP.Net and ASP.Net Core.
The default route registered by the MapDefaultControllerRoute
method is a good example of Convention
over Configuration
.
Back in chapter 2, module 3, What is ASP.Net, I talked about how when ASP.Net full framework first came out, the only framework available was WebForms, until they added MVC as an alternative several years later.
I showed a little sample of what some WebForms code might look like in that module, but I did not talk about the dreaded web.config file which was the configuration file for WebForms. The XML based configuration file was verbose, ugly, hard to read, and error prone to maintain. So much of the application functionality was hard coded and configured into the XML file which could become a maintenance nightmare (and usually did).
MVC on the other hand uses more convention then configuration. And the URL patterns described above are an example of this.
Any web developer familiar with the MVC pattern will know and understand the URLs described in the examples above and expect that behavior without having to look at a configuration file. But ASP.Net is also very flexible and custamzable. We can make other URL patterns besides the default route and assign endpoints or functionality to them.
And, the few configuration files that ASP.Net Core MVC does have are JSON (JavaScript Object Notation) files. These files are much easier to read and maintaine then XML files and make ASP.Net applications more cloud ready.
What’s Next
In this module we added the services and endpoints needed for an MVC application.
In the next module we will create our first controller called the Home Controller.