ASP.NET Core Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

Difference between IApplicationBuilder and IWebHostBuilder in ASP.NET Core

Back to: ASP.NET Core Tutorial

The difference between IApplicationBuilder and IWebHostBuilder in ASP.NET Core lies in when and how they are used during the application lifecycle. They serve different roles in configuring and initializing an ASP.NET Core application.


IWebHostBuilderBuilds and configures the web host

When it's used:

  • Before the application runs.

  • It sets up the infrastructure required to run the application (Kestrel, IIS integration, etc.).

Purpose:

  • Creates an instance of IWebHost which represents the web server and application host.

  • Used to configure:

    • Server (e.g., Kestrel)

    • Logging

    • Dependency Injection (DI) container

    • Configuration (appsettings.json, environment variables, etc.)

    • Hosting environment

Common Methods:

  • UseKestrel(): Use Kestrel web server.

  • UseStartup<T>(): Specifies the Startup class.

  • ConfigureAppConfiguration(): Add custom configuration sources.

  • ConfigureServices(): Register services in DI container.

  • Build(): Creates the IWebHost.

Think of it as:

Bootstrapping the application. It prepares everything needed before the request pipeline is constructed.


IApplicationBuilderBuilds the HTTP request pipeline

When it's used:

  • During application startup.

  • Passed into the Startup.Configure() method.

Purpose:

  • Configures the HTTP request pipeline — middleware components that handle requests/responses.

  • Does not handle the infrastructure setup.

Common Methods:

  • UseRouting()

  • UseAuthentication()

  • UseAuthorization()

  • UseEndpoints()

  • UseStaticFiles()

  • Run() / Map()

Think of it as:

Laying down the roads for HTTP requests. It defines how the application should respond to HTTP traffic.


Summary Table:

Feature IWebHostBuilder IApplicationBuilder
Purpose Configures host and services Configures request pipeline (middleware)
Used In Program.cs or Main method Startup.cs Configure method
Output IWebHost instance Middleware pipeline
Key Methods UseKestrel(), UseStartup(), Build() UseRouting(), UseEndpoints(), Run()
Concerned with Infrastructure setup HTTP request handling
Configures Hosting, DI, logging Middleware like routing, static files, auth

Typical Flow in ASP.NET Core:

  1. Program.cs:

    
        
    csharp
    public static void Main(string[] args) {
    CreateHostBuilder(args).Build().Run(); }
    public
    static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>();
    // IWebHostBuilder in action
    });
    
        
  2. Startup.cs:

    
        
    csharp
    public class Startup {
    public
    void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
    app.UseRouting();
    // IApplicationBuilder in action

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