What is the use of the Configure() method of startup class in C# Asp.net Core?

The Configure() method in ASP.NET Core's Startup class is used to define the application's request pipeline. This method configures how the application handles incoming HTTP requests and outgoing responses using middleware components.

The Configure() method is called at runtime after the ConfigureServices() method. It receives an IApplicationBuilder instance from the built-in IoC container, which is used to add middleware to the request pipeline.

Syntax

Following is the basic syntax of the Configure() method −

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Configure middleware pipeline
}

Parameters

The Configure() method accepts the following parameters −

  • IApplicationBuilder app − Provides mechanisms to configure the application's request pipeline by adding middleware components.

  • IWebHostEnvironment env − Provides information about the web hosting environment (Development, Staging, Production) the application is running in.

  • ILoggerFactory loggerFactory (optional) − Used to create loggers for the application.

How the Request Pipeline Works

ASP.NET Core Request Pipeline HTTP Request Exception Handling HTTPS Redirection Static Files Middleware Routing Middleware Authorization Middleware Endpoints (Controllers) HTTP Response Middleware executes in order, then response flows back

Using Configure() Method

Basic Configuration Example

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }

    public static void Main(string[] args)
    {
        // This is just for demonstration - normally handled by Program.cs
        System.Console.WriteLine("Configure method sets up the request pipeline");
    }
}

The output demonstrates the pipeline setup −

Configure method sets up the request pipeline

Custom Middleware Example

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // Custom middleware using Use method
        app.Use(async (context, next) =>
        {
            Console.WriteLine($"Request: {context.Request.Method} {context.Request.Path}");
            await next();
            Console.WriteLine($"Response: {context.Response.StatusCode}");
        });

        // Custom middleware using Map for specific paths
        app.Map("/api", apiApp =>
        {
            apiApp.Run(async context =>
            {
                await context.Response.WriteAsync("API Endpoint");
            });
        });

        // Default response
        app.Run(async context =>
        {
            await context.Response.WriteAsync("Hello from Configure method!");
        });
    }

    public static void Main(string[] args)
    {
        Console.WriteLine("Custom middleware configured");
        Console.WriteLine("Middleware order is important!");
    }
}

The output of the above code is −

Custom middleware configured
Middleware order is important!

Key Rules

  • Order matters − Middleware components are executed in the order they are added to the pipeline.

  • Exception handling first − Exception handling middleware should be added early in the pipeline.

  • Endpoints last − Request routing and endpoints should be configured toward the end of the pipeline.

  • Environment-specific logic − Use IWebHostEnvironment to configure different behavior for Development, Staging, and Production environments.

Conclusion

The Configure() method in ASP.NET Core defines the request pipeline by adding middleware components in a specific order. It's essential for setting up how your application handles HTTP requests, from exception handling and security to routing and response generation.

Updated on: 2026-03-17T07:04:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements