ASP.NET Core Program File Main Method in .NET 6
Lets Create an Empty Web Application in Visual Studio 2022
To create a new Empty ASP.NET Core Web Application, open Visual Studio 2022 and click on the Create a new project tab,
Once you click on the Create a new project tab, it will open the Create a new project window. From this window, you need to select the ASP.NET Core Empty project template and click the Next button,
Once you click on the Next button, it will open the Configure Your New Project window. Here, you need to provide the necessary information to create a new project. First, give an appropriate name for your project, set the location where you want to create this project, and the solution name for the ASP.NET Core Web application. And finally, click on the Create button,
Once you click on the Next button, it will open the Additional Information window. Here, you need to select .NET 6.0 as the Framework. You also need to check the Configure for HTTPS and "Do not use top-level statements" check boxes, and finally, click on the Create button
Once you click on the Create Button, the project is going to be created with the Empty template
Open the Program.cs class file, and you will see the following lines.
Do not use top-level statements checkbox,
The main method is the entry point of the ASP.NET Core Web Application. While creating the Project, if you uncheck the do not use top-level statements checkbox, then you will not find the class name and Main method name. This is a feature introduced in C# 9, where you do not have to specify a main method. This feature allows us to create one top-level file containing only statements. It will not have to declare the main method or namespace. That top-level file will become the entry point of your application.
What is Program Class?
In ASP.NET Core, the Program class is the entry point for our ASP.NET Core Web Application. It contains the application startup code where we need to
1. Configure the Web Host, i.e., to host the ASP.NET Core Web Application.
2. Configure and register the services required by the application, such as MVC, Web API, Razor Pages, etc.
3. Register Middleware Components, i.e., configure the Application Request Processing Pipeline such as Authentication, Authorization, Routing, etc.
4. Start the Application so that it can listen to HTTP Requests.
Main Method in .NET 6
The Main function has undergone a significant change since.NET 6. Here, we'll examine the function of the Main method and the Program.cs class in ASP.NET Core (.NET 6) applications.
The Main method of the Program class is the entry point of our ASP.NET Core Application, where we
1. Configure the web host.
2. Set up and register the necessary services
3. Establish middleware pipelines
4. Start the application to listen to the incoming HTTP Requests.
Understanding Program.cs Class File in ASP.NET Core
The program file creates the web application in three stages. Create, Build, and Run,
Note: The earlier versions of ASP.NET Core created two files. One is Program.cs, and the other is Startup.cs.
The Program.cs are responsible for configuring the host, and the startup class is responsible for configuring the Services and Middlewares. With .NET 6, both are merged into a Program.cs class file.
Step 1: Create (Configuring Web Server, Host, and Services)
var builder = WebApplication.CreateBuilder(args);
This line, create an instance of the WebApplicationBuilder sealed class.
The CreateBuilder is a static method of WebApplication class that accepts the Command line arguments as an input parameter. This method initializes a new instance of the WebApplicationBuilder class with preconfigured defaults such as.
1. Set up Web Server
2. Host the Application
5. Dependency Injection Container
6. Adds Framework Provided Services
Hence, the CreateBuilder method creates, initializes, and returns a new instance of the WebApplicationBuilder class. And we then use the WebApplicationBuilder instance to configure and register additional services.
Step 2: Build (Configuring the Middleware Components)
var app = builder.Build();
The Build method of the WebApplicationBuilder class creates a new instance of the WebApplication class. We then use the Web Application instance to set up the Middlewares and endpoints for our ASP.NET Core Application.
The template has set one middleware component using the MapGet extension method by default
Step 3: Run (Starts the Application)
The Run method of the WebApplication instance starts the application and listens to HTTP requests.
Using the WebApplicationBuilder instance, we will configure the services
Using the WebApplication instance, we will configure the Middleware components required for our application.