Explain the purpose of the Startup class in ASP.NET Core

The Startup class configures your application's services and defines the middleware pipeline.

Generally speaking, the Program class is where you configure your application's infrastructure, such as the HTTP server, integration with IIS, and configuration sources. In contrast, the Startup class defines which components and features your application uses and the middleware pipeline for your app.


Here is a sample Startup.cs file in a standard ASP.NET Core application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace TutorialsPoint{
   public class Startup{
      public Startup(IConfiguration configuration){
         Configuration = configuration;

      public IConfiguration Configuration { get; }

      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services){

      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
         if (env.IsDevelopment()){
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.



         app.UseEndpoints(endpoints =>{
               name: "default",
               pattern: "{controller=Home}/{action=Index}/{id?}");

The startup class contains two methods:

  • ConfigureServices(): Registers the services that your application will need.

  • Configure(): Configures the middleware pipeline that controls how the application processes the HTTP requests and sends the response


Services are modular, loosely coupled components that focus on accomplishing one task, such as caching, authentication, etc. In ASP.NET Core, services are simply C# classes that provide specific functionality to an application. 

You can use services provided by third-party Nuget libraries, or you can write them yourself. Wherever they are created, they have to be configured in the ConfigureServices() method.

The Startup class uses an IServiceCollection that holds all the services that your application will need. It also configures the dependency injection (DI). So these services are automatically injected by the DI container into your code.


Middleware defines how the application will handle the incoming HTTP requests. It also deals with the HTTP response on its way out.

Middleware consists of small modules executing in sequence to transform the incoming request or the outgoing response. The middleware can perform various tasks, including logging, authentication and authorization, service static files, error handling, etc.

One important thing to note is that the order of the middleware is important. The ASP.NET Core framework executes the middleware code in the same order in which you define it.