.Net Core Startup Class Guide


The Startup class is a single place to configure services and the app’s request pipeline.

Image by 200 Degrees from Pixabay

The Startup class explained as follows:

  1. It contains an optional Configure Services method to configure app services. A service is a reusable module that provides functionality.

  2. Need to register services in the Configure Services method and consumed throughout the application via dependency injection.

  3. Includes a Configure method to build the app’s request processing pipeline.

ConfigureServices and Configure are called by the .NET Core runtime when the app begins executing:


Specify the Startup class in the app’s host built. The Startup class is typically defined by calling the WebHostBuilderExtensions.UseStartup<TStartup> method on the host builder inside the Program.cs file as shown below.


The host provides services that are accessible to the Startup class constructor. The app attaches additional services via the ConfigureServices method. Both the host and app services are available to Configure and throughout the app.

The following services can be injected into the Startup class constructor:

  1. IWebHostEnvironment

  2. IHostEnvironment

  3. IConfiguration

Multiple Startup Classes

At runtime, the appropriate Startup class is selected when the app defines separate Startup classes for various environments.

The class whose name matches the current environment is prioritized. For example, if the app includes both a Startup class and a StartupDevelopment class, and the app is running in the Development environment, then the StartupDevelopment class will be used.

The ConfigureServices method

The ConfigureServices method is:

  1. Optional.

  2. To configure the app’s services.

  3. To set configuration options.


The Configure method

The Configure function is used to specify how the app responds to different HTTP requests. The pipeline is configured by adding middleware components.

The .NET Core templates configure the pipeline with support for:

  1. Developer Exception Page

  2. Exception handler

  3. HTTP Strict Transport Security (HSTS)

  4. HTTPS redirection

  5. Static files

  6. ASP.NET Core MVC and Razor Pages


Each Use extension method can be used to add more than one middleware components to the pipeline. For example, the UseStaticFiles method tells the middleware to serve static files.

Each middleware component in the pipeline is responsible for invoking the next element in the channel, if appropriate.

Additional services, such as

  1. IWebHostEnvironment

  2. ILoggerFactory

  3. Anything defined in ConfigureServices

Configure services without Startup

Configure services and the request processing pipeline without using a Startup class, call ConfigureServices, and Configure convenience methods on the host builder.

Multiple calls to ConfigureServices append to one another. If multiple Configure method calls exist, the last Configure call is used.


You have learned

  1. Startup class methods and their description.

  2. Multiple startup classes as per the environment.

  3. Create a .Net Core build without Startup class.

Thank you for reading. I hope you like the article..!!

#Aspnetcore #Aspnet #Csharp #DotnetCore #Dotnet

Recent Posts

See All

Design Pattern – Adapter

#Aspnetcore #AdapterDesignPattern #Csharp #DesignPatterns #Dotnet According to Gang of Four, the Adapter Pattern converts the interfaces of a class into interfaces that the client requires. In other w