What is the Role of Program.cs and Startup.cs in ASP.NET Core?
π‘ Concept Name
Program.cs and Startup.cs
π Quick Intro
In ASP.NET Core, `Program.cs` acts as the entry point of the application. `Startup.cs` is responsible for configuring services and the HTTP request pipeline. These files together define how your app starts, behaves, and serves requests.
π§ Analogy / Short Story
Think of an ASP.NET Core app as opening a restaurant. `Program.cs` is like the manager who unlocks the door, turns on the lights, and gets the place running. `Startup.cs` is like the chef and service planner β they decide what food is served and how it reaches the customer. Without the manager, nothing starts. Without the chef, nothing is served correctly.
π§ Technical Explanation
`Program.cs` contains the `Main()` method and sets up the applicationβs host environment via `CreateHostBuilder()` or `WebApplication.CreateBuilder()` in .NET 6+.
`Startup.cs` contains two main methods: `ConfigureServices()` for registering services and dependencies, and `Configure()` for setting up the middleware pipeline.
In .NET 6 and later, both responsibilities can be combined into `Program.cs` using the minimal hosting model, but the traditional separation is still common.
π― Purpose & Use Case
- β `Program.cs` defines the app's runtime environment and entry point
- β `Startup.cs` organizes service registration (`ConfigureServices()`)
- β `Startup.cs` builds the middleware request pipeline (`Configure()`)
- β Supports Dependency Injection setup and configuration loading
- β Enables clean separation of app setup and request handling logic
π» Real Code Example
Program.cs (.NET 5)
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
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
}

β Interview Q&A
Q1: What is Program.cs in ASP.NET Core?
A: It's the entry point of the app and sets up the web host.
Q2: What does Startup.cs do?
A: It configures services and defines the middleware pipeline.
Q3: Can you remove Startup.cs in .NET 6+?
A: Yes, using minimal hosting in `Program.cs`.
Q4: What is `ConfigureServices()` used for?
A: To register services and dependencies with the DI container.
Q5: What is `Configure()` used for?
A: To define how HTTP requests are processed by middleware.
Q6: Where do you configure routing?
A: Inside `Configure()` using `UseRouting()` and `MapControllers()`.
Q7: Can we use multiple Startup classes?
A: Not directly, but conditional startup can be achieved via `UseStartup
Q8: What happens if Startup.cs is missing?
A: The app will not know how to configure services or the request pipeline.
Q9: Is Startup.cs mandatory?
A: In .NET 5 and earlier, yes. In .NET 6+, no (optional with minimal hosting).
Q10: Whatβs the advantage of separating Program and Startup?
A: Better organization and testability of bootstrapping logic vs. service logic.
π MCQs
Q1: What is the entry point of an ASP.NET Core application?
- A. Program.cs
- B. Startup.cs
- C. Global.asax
- D. appsettings.json
Q2: Which method is used to add services in Startup.cs?
- A. ConfigureServices()
- B. Configure()
- C. Main()
- D. AddServices()
Q3: Where is middleware configured?
- A. Startup.cs β Configure()
- B. Program.cs
- C. appsettings.json
- D. Web.config
Q4: What file contains the `Main()` method?
- A. Program.cs
- B. Startup.cs
- C. appsettings.json
- D. Configure.cs
Q5: What command creates a new ASP.NET Core web app?
- A. dotnet new webapi
- B. dotnet new startup
- C. dotnet start
- D. dotnet build
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!