Total Page Preview:   000000000206

Explain Program.cs file and Startup.cs file in Asp.Net Core MVC

Program.cs file 

The Program.cs file in an ASP.NET Core MVC application serves as the entry point for the application. It's where the application's execution starts. The file contains the Main method, and its primary responsibility is to create and configure the web host that will run the application.

Here's a breakdown of the key components and responsibilities of the Program.cs file in an ASP.NET Core MVC application:
1-Main Method: The Main method is the entry point of the application. It initializes and starts the web host. This method is responsible for creating an instance of the host using the CreateHostBuilder method and then invoking the Run method to start the application's execution.
2-Host Creation: The CreateHostBuilder method is usually a static method that creates and configures an instance of the IHostBuilder. The IHostBuilder is used to set up the configuration and services required by the application.
3-Default Host Configuration: The CreateHostBuilder method often starts with the Host.CreateDefaultBuilder(args) call. This sets up common configurations such as logging, configuration sources, and dependency injection.
4-WebHost Configuration: Within the CreateHostBuilder method, there's usually a call to ConfigureWebHostDefaults to configure the web host. This includes specifying the startup class (Startup) that configures services and middleware for the application.
5-Startup Class: The UseStartup<Startup>() method call inside the ConfigureWebHostDefaults is crucial. It indicates the Startup class where you configure services, middleware, and application behavior.
 
Here's an example of how a typical Program.cs file might look in an ASP.NET Core MVC application:
 
Key points:
1-The Main method is the entry point of the application. It's where the web host is created and started.
2-Host.CreateDefaultBuilder(args) creates a default IHostBuilder that configures various aspects of the application, such as logging and configuration.
3-.ConfigureWebHostDefaults configures the web host. Inside it, you specify the startup class for your application (usually named Startup) using UseStartup<Startup>().
The Startup class referenced in the UseStartup<Startup>() method is another critical part of the ASP.NET Core application. It's responsible for configuring the application's services, middleware pipeline, and routes for MVC controllers.
In summary, the Program.cs file in an ASP.NET Core MVC application initializes and starts the application's web host, while the Startup class configures the services and middleware required for the application to run, including MVC-related configurations.
 
Startup.cs file
Connecting to a database and configuring services in the Program.cs file is not a common practice in ASP.NET Core. Instead, database connections and service configurations are usually handled within the Startup.cs class. However, I can provide you with an example of how to configure a database connection and add a service in the Startup.cs file:
In this example, ApplicationDbContext represents your Entity Framework database context class. You would typically define this class to manage interactions with your database.
ISomeService and SomeService are just placeholders for a service interface and implementation that you might have in your application.
Remember, the Program.cs file generally doesn't deal with database connections and service configurations directly. It's more focused on creating the web host and setting up the application's initial configuration. The actual service registrations and database connections are handled in the Startup.cs file.
 
 
 
 
 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments


                           
                           

                           

                           

Facebook User: