liveBook · Manning

WebApplicationBuilder

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

WebApplicationBuilder is the ASP.NET Core builder type that configures application services, logging, and other hosting concerns before constructing a web application.

WebApplicationBuilder is the setup object for an ASP.NET Core application. In Program.cs, call WebApplication.CreateBuilder(args), configure the builder, then call Build() to create a WebApplication. This delays creation of the application until its setup is complete. section 3.6

Even if you change nothing, the builder sets up configuration from JSON files and environment variables, logging, and services. Use builder.Services to register framework features and your own application services before Build(). Registered services can then be supplied to classes that depend on them through dependency injection. section 3.6 section 3.7.1

Add HTTP request logging

Call builder.Services.AddHttpLogging(...) to register and configure HTTP logging, and builder.Logging.AddFilter(...) to set its log level. Then call Build(). Add app.UseHttpLogging() to the resulting WebApplication when running in Development. The builder prepares the feature; the application adds its middleware to the request pipeline. section 3.7 section 3.7.1

Listing 3.4 The Program.cs file for a more complex example minimal API
using Microsoft.AspNetCore.HttpLogging;
 
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
 
builder.Services.AddHttpLogging(opts =>                         #1
    opts.LoggingFields = HttpLoggingFields.RequestProperties);  #1
 
builder.Logging.AddFilter(                                      #2
    "Microsoft.AspNetCore.HttpLogging", LogLevel.Information);  #2
 
WebApplication app = builder.Build();
 
if (app.Environment.IsDevelopment())                            #3
{
    app.UseHttpLogging();                                       #4
}
 
app.MapGet("/", () => "Hello World!");
app.MapGet("/person", () => new Person("Andrew", "Lock"));      #5
 
app.Run();
 
public record Person(string FirstName, string LastName);        #6

The complete Program.cs example shows the order: create the builder, configure services and logging, build the application, add middleware and endpoints, then run it. Its /person endpoint returns a JSON representation of a Person record. section 3.7

Keep builder setup separate from WebApplication setup. After Build(), add middleware, map endpoints, and call Run() to start handling requests. The same sequence applies to Razor Pages: register the feature with builder.Services.AddRazorPages(), then map its pages on the WebApplication. section 3.8 section 13.1.2

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

How deep it goes
Works through the create-configure-build sequence and service registration, while deferring detailed treatment of dependency injection, configuration, and logging.
Examples
Uses a minimal API with HTTP request logging and a JSON /person endpoint, then a Razor Pages setup.
What it assumes
Starts with a small Program.cs application and introduces service registration before referring detailed dependency-injection mechanics elsewhere.
What you can do after
Build it from scratch by configuring a builder, calling Build(), then adding middleware and endpoints before Run().
How it connects
Connects WebApplicationBuilder to WebApplication, dependency injection, logging, middleware, endpoints, minimal APIs, and Razor Pages.

Also explained in

Compare how 2 books explain WebApplicationBuilder →

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.