ASP.Net

Web Application Builder

bild.png
bild.png
bild.png
bild.png

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();


Adding Configuration using builder.Configuration.
Adding Services using builder.Services
Configure Logging using builder.Logging
General IHostBuilder and IWebHostBuilder configuration

WebApplicationBuilder exposes the ConfigurationManager type for adding new configuration sources, as well as accessing configuration values, as .

It also exposes an IServiceCollection directly for adding services to the DI container. So whereas with the generic host you had to do

var hostBuilder = Host.CreateDefaultBuilder(args);
hostBuilder.ConfigureServices(services =>
{
services.AddRazorPages();
services.AddSingleton<MyThingy>();
})


with WebApplicationBuilder you can do
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddSingleton<MyThingy>();


Similarly, for logging, instead of doing
var hostBuilder = Host.CreateDefaultBuilder(args);
hostBuilder.ConfigureLogging(builder =>
{
builder.AddFile();
})


you would do:
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddFile();


This has exactly the same behaviour, just in an easier-to-use API. For those extension points that rely on IHostBuilder or IWebHostBuilder directly, WebApplicationBuilder exposes the properties Host and WebHost respectively.
For example, hooks into the IHostBuilder, so in ASP.NET Core 3.x/5 you would add it using the following:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() // <-- Add this line
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

With WebApplicationBuilder, you would make the UseSerilog() call on the Host property, instead of on the builder itself:
builder.Host.UseSerilog();


In fact, WebApplicationBuilder is where you do all your configuration except the middleware pipeline.

WebApplication wears many hats


Once you've configured everything you need to on WebApplicationBuilder you call Build() to create an instance of WebApplication:
var app = builder.Build();

WebApplication is interesting as it implements multiple different interfaces:
Those latter two points are very much related. In ASP.NET Core 3.x and 5, the IEndpointRouteBuilder is used to add endpoints by calling UseEndpoints() and passing a lambda to it, for example:
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}


There's a few complexities to this .NET 3.x/5 pattern for people new to ASP.NET Core:
The middleware pipeline building occurs in the Configure() function in Startup (you have to know to look there)
You have to make sure to call app.UseRouting() before app.UseEndpoints() (as well as place other middleware in the right place)
You have to use a lambda to configure the endpoints (not complicated for users familiar to C#, but could be confusing to newcomers)

WebApplication significantly simplifies this pattern:
app.UseStaticFiles();
app.MapRazorPages();



Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.