The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
In ASP.NET Core, the Program.cs and Startup.cs files play a crucial role in
configuring and bootstrapping your application. Here's a clear explanation of their purposes:
Program.cs – Application Entry Point
Purpose:
Defines the main entry point of the application.
Responsible for creating and running the web host (the environment in which the app runs).
Responsibilities:
Configures the host (web server, logging, etc.)
Calls the Startup class to set up services and middleware.
Example (.NET Core 3.1 style):
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Startup.cs – App Configuration
Purpose:
Contains methods to configure services and the HTTP request pipeline.
Key Methods:
ConfigureServices(IServiceCollection services) Register app services, e.g., MVC, EF Core, authentication, etc.
Configure(IApplicationBuilder app, IWebHostEnvironment env) Set up the
middleware pipeline (e.g., routing, error handling, static files).
Example:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
}
}
In .NET 6/7+, Program.cs Merges Everything
In .NET 6 and newer, Startup.cs is optional. The
Program.cs file uses a minimal hosting model, combining everything in one place:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.MapDefaultControllerRoute();
app.Run();
Summary
File
Purpose
Used In
Program.cs
Entry point, host setup
All ASP.NET Core apps
Startup.cs
Configure services and middleware
.NET Core 2.x – 5.x (optional in .NET 6+)
Program.cs (Minimal API style)
Combines both responsibilities
.NET 6 and newer
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In ASP.NET Core, the
Program.csandStartup.csfiles play a crucial role in configuring and bootstrapping your application. Here's a clear explanation of their purposes:Program.cs– Application Entry PointPurpose:
Responsibilities:
Startupclass to set up services and middleware.Example (
.NET Core 3.1style):Startup.cs– App ConfigurationPurpose:
Key Methods:
ConfigureServices(IServiceCollection services)Register app services, e.g., MVC, EF Core, authentication, etc.Configure(IApplicationBuilder app, IWebHostEnvironment env)Set up the middleware pipeline (e.g., routing, error handling, static files).Example:
In .NET 6/7+,
Program.csMerges EverythingIn .NET 6 and newer,
Startup.csis optional. TheProgram.csfile uses a minimal hosting model, combining everything in one place:Summary
Program.csStartup.csProgram.cs(Minimal API style)