Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the purpose of Program.cs file in C# ASP.NET Core project?
The Program.cs file in ASP.NET Core serves as the entry point for your web application. It contains the Main() method that bootstraps and configures the web host, essentially turning your web application into a console application that can be executed.
Purpose and Structure
ASP.NET Core web applications are console applications at their core. The Program.cs file is responsible for −
Creating and configuring the web host
Setting up default configurations and services
Specifying the startup class
Running the application
Syntax
Following is the basic structure of Program.cs in ASP.NET Core −
public class Program {
public static void Main(string[] args) {
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Example Implementation
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System;
public class Program {
public static void Main(string[] args) {
Console.WriteLine("Starting ASP.NET Core application...");
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
public class Startup {
public void ConfigureServices(IServiceCollection services) {
// Add services here
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapGet("/", async context => {
await context.Response.WriteAsync("Hello from ASP.NET Core!");
});
});
}
}
The output when running this application would be −
Starting ASP.NET Core application...
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
Key Components
WebHost Class
The WebHost static class creates instances of IWebHost and IWebHostBuilder with pre-configured defaults.
CreateDefaultBuilder() Method
The CreateDefaultBuilder() method sets up essential configurations automatically −
Sets the content root to the current directory
Enables command line arguments in configuration
Loads appsettings.json and environment-specific settings
Adds environment variables to configuration
Enables user secrets in development
Configures console and debug logging
Sets up Kestrel web server
Adds routing capabilities
Integrates with IIS when deployed
UseStartup() Method
The UseStartup<T>() method specifies which class contains the application's startup configuration. You can use a custom startup class instead of the default Startup class.
Build() and Run() Methods
The Build() method creates the IWebHost instance, while Run() starts the web application and keeps it running until stopped.
Advanced Configuration Example
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System;
public class Program {
public static void Main(string[] args) {
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://localhost:8080")
.UseIISIntegration()
.Build();
}
public class Startup {
public void ConfigureServices(IServiceCollection services) {
// Configure services
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapGet("/", async context => {
await context.Response.WriteAsync("Custom configured ASP.NET Core app running on port 8080!");
});
});
}
}
IIS Integration
When hosting in IIS, the UseIISIntegration() method configures the application to work with IIS as a reverse proxy. This is automatically included in CreateDefaultBuilder().
Conclusion
The Program.cs file is the foundation of every ASP.NET Core application, serving as the entry point that configures the web host and starts the application. It provides a clean separation between application bootstrapping and business logic configuration, making ASP.NET Core applications highly configurable and testable.
