ASP.NET Core

Understand Main Method in in .NET 6 Asp.net Core

Understand Main Method in in .NET 6 Asp.net Core

Main Method in in .NET 6 Asp.net Core

Role of Main Method in ASP.NET Core:
The Main Method of the Program.cs class is the entry point of our ASP.NET Core Application. It configures and builds the Web host. The Web Host is responsible for running our ASP.NET Core Applications. It does so by calling the CreateBuilder method, and most of the code required to configure the Web Host is already written for us within the CreateBuilder method.

First, the Main Create the WebApplicationBuilder instance using which we can configure required services for our ASP.NET Core Application.
Second, the Main method creates an instance of the WebApplication class, using which we can configure required Middleware Components or EndPoints, which will handle the incoming HTTP Requests.
Third, the Main method starts the application by calling the Run method.

Then The CreateBuilder method sets the web host with default configurations, such as it will host the application with the default server (i.e., either IIS or Kestrel) and with the default hosting model (i.e., InProcess or OutOfProcess). So, next, we will learn Different Hosting Models.



WebApplicationBuilder is a 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
3. Logging
4. Configuration
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.


The WebApplicationBuilder exposes the following.

1. IWebHostEnvironment: It provides information about an application’s web hosting environment. It will provide information on whether the application is hosted using In-Process or OutOfProcess Hosting Model. Whether it is using IIS Server or Kestrel Web Server, or both.

2. IServiceCollection: A collection of services that can be used throughout an application. This is useful for adding user-provided or framework-provided services. It provides methods to register different types of services, such as Transient, Scoped, and Singleton services.

3. ConfigurationManager: A collection of configuration providers that can be used throughout an application. This is useful for adding new configuration sources and providers.

4. ILoggingBuilder: A collection of logging providers that can be used throughout an application. This is useful for adding new logging providers.



MapGet Method in ASP.NET Core:
In ASP.NET Core, routing is in charge of matching incoming HTTP requests (URLs) with the appropriate application's executable endpoints, who will then process the request.
Endpoints are nothing but the application’s executable code that will execute for Handling the Requests
The code in the below sets up a single endpoint using the MapGet function. With the MapGet endpoint, the statement () => "Hello World!" will run and Hello World! is written to the HTTP response when an HTTP GET request is performed to the application root URL /.

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        var app = builder.Build();

        app.MapGet("/", () => "Hello World!");

        app.Run();
    }
}






Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram