---
title: "What is the use of the Program.cs and Startup.cs files in ASP.NET Core?"  
description: "What is the use of the Program.cs and Startup.cs files in ASP.NET Core?"  
author: "Anubhav Sharma"  
published: 2025-05-19  
updated: 2025-05-19  
canonical: https://www.mindstick.com/interview/34140/what-is-the-use-of-the-program-cs-and-startup-cs-files-in-asp-dot-net-core  
category: "c#"  
tags: ["c#", ".net core"]  
reading_time: 4 minutes  

---

# What is the use of the Program.cs and Startup.cs files in ASP.NET Core?

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:

1. Defines the **main entry point** of the application.
2. Responsible for **creating and running the web host** (the environment in which the app runs).

### Responsibilities:

1. Configures the **host (web server, logging, etc.)**
2. Calls the `Startup` class to **set up services and middleware**.

### Example (`.NET Core 3.1` style):

```cs
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:

1. Contains methods to **configure services** and the **HTTP request pipeline**.

### Key Methods:

1. `ConfigureServices(IServiceCollection services)` Register app services, e.g., MVC, EF Core, authentication, etc.
2. `Configure(IApplicationBuilder app, IWebHostEnvironment env)` Set up the **middleware pipeline** (e.g., routing, error handling, static files).

### Example:

```cs
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:

```cs
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 |

## Answers

### Answer by Anubhav Sharma

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:

1. Defines the **main entry point** of the application.
2. Responsible for **creating and running the web host** (the environment in which the app runs).

### Responsibilities:

1. Configures the **host (web server, logging, etc.)**
2. Calls the `Startup` class to **set up services and middleware**.

### Example (`.NET Core 3.1` style):

```cs
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:

1. Contains methods to **configure services** and the **HTTP request pipeline**.

### Key Methods:

1. `ConfigureServices(IServiceCollection services)` Register app services, e.g., MVC, EF Core, authentication, etc.
2. `Configure(IApplicationBuilder app, IWebHostEnvironment env)` Set up the **middleware pipeline** (e.g., routing, error handling, static files).

### Example:

```cs
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:

```cs
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 |


---

Original Source: https://www.mindstick.com/interview/34140/what-is-the-use-of-the-program-cs-and-startup-cs-files-in-asp-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
