Part - 3 ASP - Net Core Main Method
Part - 3 ASP - Net Core Main Method
The Main method of the Program class is the entry point of our ASP.NET Core Application, where we
configure the host. We also configure and register required services and configure middleware pipelines
inside the Main method of the Program class.
Once you click on the Create a new project box, it will open the “Create a new project” window. This
window includes different .NET 6 application templates. Here we will create a simple Web application
from scratch, so select the ASP.NET Core Empty Project template and then click on the Next button
as shown in the below image.
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 as shown in the below image.
Once you click on the Create button, it will create a new ASP.NET Core Web Application in Visual Studio
2022 using .NET 6 with the following file and folder structure.
Open the Program.cs class file and you will see the following lines. As you can see the Main Method of
the Program class contains four lines of code and these four lines of code contain all the initialization
code that is required to create a web server, host the application and start listening for HTTP requests.
The program file creates the web application in three stages. Create, Build, and Run as shown in the
below image.
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. Let us understand the Program file code in detail.
We then call the Build method using the WebApplicationBuilder instance. As we progress in this course,
we will see how we can configure different types of built-in services like MVC, Web API, Razor Page,
etc. as well as we will also discuss how to Custom Services and configure them.
Note: Using the WebApplicationBuilder instance, we will configure the services, and using the
WebApplication instance, we will configure the Middleware components required for our application. As
we progress in this course, we will discuss How to configure services and middleware components in
.NET 6 Application in detail.
app.Run();
}
}
}
The above example code configures a single endpoint using the MapGet method. With the MapGet
endpoint, when an HTTP GET request is sent to the application root URL /, then the request delegate
To prove that it is a GET request, open the browser developer tool by pressing the F12 key and then
go to the Network tab and refresh the page again. Then click on the local host and you will see
the Request URL as https://localhost:7279/ and the Request Method as GET as shown in the below
image.
With the MapGet endpoint, we can only send HTTP GET requests to the server from the application
root URL /. Now, let us add something to the URL and press the enter button as shown in the below
image.
Next=> The CreateBuilder method set web host with default configurations such as it will host the
application with default server (i.e. either IIS or Kestrel) and with default hosting model (i.e. InProcess
or OutOfProcess). So, next we will learn Different Hosting Models.