In the ASP.NET Core project, we have a file called program.cs. In this file, we have a public static void Main() method.

If you are interested in traditional. Any experience with the NET Framework knows that console applications have the Main() method, which is the entry point to that console program.

But today, we’re creating an ASP.NET Core Web application instead of a console application. So the obvious question that comes to mind is. Why do we also have a Main() method?

So this is something to keep in mind. The ASP.NET Core application starts as a console application, and the Main() method in the program.cs file is the entry point.

So, when the runtime executes our application, it looks for this Main() method and where the execution configuration started.

The Main() method configures ASP.NET Core and launches it, at which point it becomes an ASP.NET Core Web application. So, if you trace the Main() method, it calls the CreateHostBuilder method passing the command-line arguments.

Then you can see that the CreateHostBuilder() method returns an object that implements IHostBuilder. Calling the Build() method on this object will generate and host our ASP.NET Core application on the server. The program on the server calls the Run() method, which runs the Web application and starts listening for incoming HTTP requests. The CreateHostBuilder method calls the static method CreateDefaultBuilder() in the static class Host. The CreateDefaultBuilder() method creates a preset default value on the server. The CreateDefaultBuilder() method performs several actions to create the server.

Understand that the CreateDefaultBuilder() method exists to create the default values for the program configuration on the server. As part of setting up the server, it also uses the extension method of UseStartup() in the IHostBuilder interface to configure the Startup class.

By Microsoft’s rules, the Startup class in ASP.NET Core is called Startup. This class has two methods.

  • ConfigureServices()Method to configure the services required by the application
  • Configure()Method to configure the application’s request processing pipeline