Header add

In this article we discuss the ASP.NET Core Main Method in detail. Please read our previous article where we discussed the ASP .NET Core MVC Project Structure & Process Flow.
  • What the use of ASP.NET Core Main Method.
  • Why do we have a Main() method in ASP.NET Core ?
  • What happens behind the scenes when you run a .NET core application ?
Let's Create a .NET Core application form scratch and discuss about all these things.


Open Visual Studio ⇒ File ⇒ New Project


Choose “Visual C#” and then select .NET Core. From the middle pane, you need to choose ASP.NET Core Web Application. Provide a meaningful name for your application. Then provide the location where you want to create the Project. Finally, click on the OK button as shown in the image below.


Once you click on the OK button, it will open the following window. From the below window, select ASP.NET Core 2.2 (currently it is the latest version). Then uncheck the Configure for HTTPS checkbox. Then click on the OK button as shown below.

After clicking the "OK" button the .NET Core application is open for you like below;

As you can see from the image, we have a file with the name Program.cs. The Program.cs file of our ASP.NET Core Web Application contains the following code.
When you open the Program.cs file you can see that the Program class contains a public static void Main() method. As we already know, when we create a console application in .net then by default the .NET Framework creates a class (i.e. Program class) with the Main Method. We also know that the Main() method is the entry point for that console application execution.

Now the question is, here we are not creating a console application, here we create an ASP.NET Core Web Application. Then why do we have a Main() method in the ASP.NET Core Web Application ? Let's discuss that in details.

Why do we have a Main() method in ASP.NET Core ?

The most important point that you need to keep in mind is, the ASP.NET Core Application initially starts as a Console Application and the Main() method is the entry point to the application.

So, when we execute the ASP.NET Core application, first it looks for the Main() method and this is the method from where the execution starts. The Main() method then configures the ASP.NET Core and starts it. At this point, the application becomes an ASP.NET Core web application.

If you look at the body of the Main() method, then you will find that it makes a call to the CreateWebHostBuilder() method by passing the command line arguments as shown in the below image.

As shown in the below image, the CreateWebHostBuilder() method returns an object that implements the IWebHostBuilder interface.

Within the Main() method, on this IWebHostBuilder object, the Build() method is called which actually builds a web host. Then it hosts our asp.net core web application within that Web Host.

Finally, on the web host, we called the Run() method, which actually runs the web application and it starts listening to the incoming HTTP requests.

CreateWebHostBuilder() method calls the static CreateDefaultBuilder() method  of the WebHost class. The CreateDefaultBuilder() method creates a web host with the default configurations.

Startup Class

While setting up the web host, the Startup class is also configured using the UseStartup() extension method of the IWebHostBuilder class. The Startup class has two methods as shown below.
  • ConfigureServices() method configures services required by the application
  • Configure() method sets up the application's request processing pipeline
We will discuss later in details of the above two methods.

 In this tutorial we discussed Main method in ASP.NET Core. If have any question related to this topic then give your feedback.

You May Also Like...

Post a Comment

Previous Post Next Post