liveBook · Manning

Startup class

As explained in ASP.NET Core in Action, 3e

A Startup class is an ASP.NET Core class that defines application service configuration and request-pipeline configuration during startup.

A Startup class defines an ASP.NET Core app’s services and request-handling behavior when the app uses the generic host. It keeps feature configuration in Startup.cs, while Program.cs creates and runs the host and configures infrastructure such as Kestrel and configuration sources. section 30.1 section 30.2

Listing 30.1 The Program.cs file configures and runs an IHost
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args)                                #1
            .Build()                                           #2
            .Run();                                            #3
    }
 
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)                        #4
            .ConfigureWebHostDefaults(webBuilder =>            #5
            {
                webBuilder.UseStartup<Startup>();              #6
            });
}

Program.cs calls UseStartup<Startup>() while configuring the host, then builds and runs it. This is the link that makes the Startup class part of application bootstrapping. section 30.2

Startup has two main methods, called in this order:

  • ConfigureServices(IServiceCollection services) registers services with dependency injection. For example, a Razor Pages app can register its database context, identity services, an application service, and Razor Pages. section 30.3
  • Configure(IApplicationBuilder app) assembles the middleware pipeline and maps endpoints. Services registered in ConfigureServices are available by the time Configure runs. section 30.3
Listing 30.4 Startup.Configure() for a Razor Pages application
public class Startup
{
    public void Configure(
        IApplicationBuilder app,              #1
        IWebHostEnvironment env)              #2
    {
        if (env.IsDevelopment())              #3
        {
            app.UseDeveloperExceptionPage();  #4
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }
 
        app.UseHttpsRedirection();
        app.UseStaticFiles();
 
        app.UseRouting();                     #5
 
        app.UseAuthentication();  
        app.UseAuthorization();               #6
 
        app.UseEndpoints(endpoints =>         #7
        {
            endpoints.MapRazorPages();        #8
        });
    }
}

A Razor Pages example shows Configure selecting development or production error handling, adding request middleware, and mapping Razor Pages endpoints. section 30.3

Startup does not normally need to implement an interface. The host finds its conventionally named methods using reflection. Its constructor can receive IConfiguration or IWebHostEnvironment; Configure can receive registered services through dependency injection. Implementing IStartup or deriving from StartupBase gives fixed method signatures, but removes that Configure injection option. section 30.3 section 30.7

This remains a supported choice in .NET 7, particularly for an existing app or one needing fine control over host creation. For most new web apps, the simpler WebApplication-based minimal hosting approach is the default. Splitting bootstrapping across Program.cs and Startup.cs can make configuration harder to trace. section 30.6 section 30.5

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains method order, dependency injection, and reflection-based conventions, with service and middleware listings.
Examples
Uses a Razor Pages app to show service registration and a request pipeline with error handling, authentication, and endpoint mapping.
What it assumes
Introduces the two-file hosting arrangement before explaining Startup’s methods; assumes basic familiarity with C# method declarations.
What you can do after
Operate/configure it by registering services in ConfigureServices and assembling middleware and endpoints in Configure.
How it connects
Connects dependency injection, middleware, endpoint routing, IHostBuilder, and minimal hosting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.