Skip to content

4.4 — The Legacy Startup.cs Model

← Roadmap · Section: 4. Fundamentals and Application Startup Prev: 4.3 Program.cs · Next: 4.5 Host interfaces

Markers: ⭐ · Confidence: 🔴 · Last reviewed: —


TL;DR

Startup.cs is the older way of arranging application setup, used from ASP.NET Core 1.0 until .NET 6. It splits the same two phases into two methods on a separate class.

  • ConfigureServices registers services. Configure builds the middleware pipeline. Exactly the same two phases as minimal hosting.
  • It still works today — it was never removed, only replaced as the default.
  • You need to recognise it because most existing production code still uses it.

Definitions

Term What it is What it means
Startup.cs a class, by convention Holds the two setup methods. Not an interface — the framework finds the methods by name.
ConfigureServices a method Phase 1. Registers services into the DI container. Equivalent to code before builder.Build().
Configure a method Phase 2. Builds the middleware pipeline. Equivalent to code after builder.Build().
IApplicationBuilder an interface What Configure receives. You call Use... on it to add middleware.
IHostBuilder an interface The Generic Host builder. Host.CreateDefaultBuilder(args) returns one.
UseStartup<T> a method Tells the host which class holds the setup methods.
IWebHostEnvironment an interface Tells you the environment name and content root. Injectable into Configure.
Convention-based a discovery style The framework looks for methods by name, not by an interface. Misspell one and it is silently ignored.
ConfigureContainer an optional method Used when swapping in a third-party DI container like Autofac.
Environment-specific methods a convention ConfigureDevelopmentServices runs instead of ConfigureServices in Development.

The concept

Why it existed

When ASP.NET Core launched in 2016, C# had no top-level statements. Every program needed a Main method inside a class, so the entry point was already boilerplate-heavy.

Rather than cram all setup into Main, the design put it in a dedicated class with two clearly named methods. That was a reasonable separation: Program.cs for "how do I start", Startup.cs for "what am I".

By .NET 6 the language had changed. Top-level statements and implicit usings removed the boilerplate, so the extra file was no longer buying anything — and having setup split across two files made it harder to follow. Minimal hosting merged them.

The important thing to understand: the two phases did not change. Only where you write them did.


How it works

The simple version — the two files

// Program.cs — just wires up the host and points at Startup
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>();     // ← the link
            });
}
// Startup.cs — the actual application setup
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;      // injected by the framework
    }

    public IConfiguration Configuration { get; }

    // ═══ PHASE 1 ═══ register services
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddDbContext<AppDbContext>(o =>
            o.UseSqlServer(Configuration.GetConnectionString("Default")));
        services.AddScoped<IOrderService, OrderService>();
    }

    // ═══ PHASE 2 ═══ build the pipeline
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();

        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

Side by side with minimal hosting

Startup model Minimal hosting
services.AddControllers(); in ConfigureServices builder.Services.AddControllers();
app.UseAuthentication(); in Configure app.UseAuthentication();
Configuration property builder.Configuration
IWebHostEnvironment env parameter app.Environment
app.UseEndpoints(e => e.MapControllers()) app.MapControllers()

The mapping is almost mechanical, which is why migrating is usually straightforward: move the body of ConfigureServices above builder.Build(), move the body of Configure below it, and rename services to builder.Services.

⚠️ Convention-based, not interface-based

This is the trap worth knowing. Startup implements no interface. The framework finds the methods by name, using reflection.

public class Startup
{
    // Typo — the framework never finds this.
    public void ConfigureService(IServiceCollection services)   // missing 's'
    {
        services.AddControllers();
    }
}

That compiles perfectly. It just never runs. Your app starts, and then every request fails with an unhelpful error about no controllers being found — because none were registered.

There is no compile-time protection at all. Minimal hosting removed this whole class of problem, since there are no magic method names to misspell.

How it actually works — environment-specific methods

A lesser-known convention: you can define per-environment versions, and the framework prefers them:

public class Startup
{
    // Used in Development instead of ConfigureServices
    public void ConfigureDevelopmentServices(IServiceCollection services)
    {
        services.AddDbContext<AppDbContext>(o => o.UseInMemoryDatabase("dev"));
        ConfigureCommonServices(services);
    }

    // Used everywhere else
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<AppDbContext>(o =>
            o.UseSqlServer(Configuration.GetConnectionString("Default")));
        ConfigureCommonServices(services);
    }

    // And the pipeline equivalent
    public void ConfigureDevelopment(IApplicationBuilder app, IWebHostEnvironment env) { }
}

The naming is Configure{Environment}Services and Configure{Environment}. It works, but it is obscure enough that most teams use env.IsDevelopment() checks instead — which is clearer and does not depend on a naming convention nobody remembers.

The deeper detail — UseRouting and UseEndpoints

Old Startup code has a pattern that has since been simplified:

app.UseRouting();                          // "work out which endpoint matches"

app.UseAuthentication();
app.UseAuthorization();                     // these sit BETWEEN the two

app.UseEndpoints(endpoints =>               // "now actually run it"
{
    endpoints.MapControllers();
});

The split exists because authorization needs to know which endpoint was selected before it can decide whether you are allowed to reach it. UseRouting selects; UseEndpoints executes; authorization sits in between with the answer available.

In minimal hosting that is implicit:

app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();          // routing and endpoints are added automatically

WebApplication inserts UseRouting at the start and UseEndpoints at the end for you. You can still call them explicitly if you need something to sit at an exact position.

See 8.2.


Code

Should you migrate existing code?

Usually not, on its own. A working Startup.cs is not a bug. Migrating is a mechanical change with a small chance of subtly reordering middleware, and the benefit is stylistic.

Reasonable reasons to migrate: - You are already doing a major upgrade and want consistency. - New developers find the split confusing. - You are adopting minimal APIs and want everything in one place.

If you do, the migration is:

// BEFORE — Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddScoped<IOrderService, OrderService>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(e => e.MapControllers());
}
// AFTER — Program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddScoped<IOrderService, OrderService>();

var app = builder.Build();

if (app.Environment.IsDevelopment()) app.UseDeveloperExceptionPage();
app.UseAuthorization();
app.MapControllers();

app.Run();

Keep the middleware in exactly the same order. That is the one part where a mistake causes real behaviour changes rather than a compile error.

Keeping Startup while using the new builder

A middle option that is genuinely useful during a gradual migration — you can keep the Startup class and call its methods from minimal hosting:

var builder = WebApplication.CreateBuilder(args);

var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);

var app = builder.Build();

startup.Configure(app, app.Environment);

app.Run();

This lets you move to the new host without rewriting a large Startup in one go.


Interview questions

Q: What is Startup.cs and is it still used?

It is the older way of organising ASP.NET Core setup, used from 1.0 until .NET 6. It holds two methods: ConfigureServices, which registers services into the DI container, and Configure, which builds the middleware pipeline. Those are exactly the same two phases as minimal hosting — before and after builder.Build() — just arranged as two methods on a separate class. It was never removed, so it still works, and most existing production codebases use it, which is why it is worth recognising.

Q: What is unusual about how the framework finds those methods?

They are discovered by convention rather than through an interface — the framework looks them up by name using reflection. So Startup implements nothing, and if you misspell ConfigureServices the code compiles fine and the method simply never runs. The app then starts with no services registered and fails at the first request with a confusing error. That whole class of problem disappeared with minimal hosting, because there are no magic method names any more.

Q: Why did old code have UseRouting and UseEndpoints as separate calls?

Because authorization needs to know which endpoint matched before it can decide whether the caller is allowed to reach it. UseRouting performs the match, UseEndpoints executes the selected endpoint, and authentication and authorization sit between them with the routing result available. In minimal hosting WebApplication adds both automatically — UseRouting at the start of the pipeline and UseEndpoints at the end — so you just call MapControllers. You can still add them explicitly if you need middleware at an exact position relative to routing.

Q: Would you migrate an existing Startup.cs to minimal hosting?

Not on its own. It works, and the change is stylistic rather than functional, so it is gratuitous risk on a working system. I would do it as part of a major upgrade or if the team was already moving towards minimal APIs. The mapping is nearly mechanical — the body of ConfigureServices goes above Build() and the body of Configure goes below — but the one thing to be careful about is preserving the exact middleware order, because getting that wrong changes behaviour without producing a compile error.


Traps ⚠️

  • Method names are matched by convention, not an interface. A typo compiles and silently does nothing.
  • ConfigureServices and Configure have fixed signatures. Configure can take extra injected services; ConfigureServices cannot.
  • Only services can be injected into the Startup constructor — and only IConfiguration, IWebHostEnvironment and a few others, because the container is not built yet.
  • Migrating can silently reorder middleware. That changes behaviour with no compile error.
  • Environment-specific method names are obscure. ConfigureDevelopmentServices works, and almost nobody knows it exists, so prefer explicit environment checks.
  • UseEndpoints without UseRouting before it throws in the old model.


Sources