Wednesday, September 25, 2024

WebApplicationBuilder and WebApplicationOptions in ASP.NET Core

Objectives:

  • Usage of WebApplicationBuilder
  • Usage of WebApplicationOptions
  • Difference between WebApplicationBuilder and WebApplicationOptions

WebApplicationBuilder and WebApplicationOptions are used in configuring and building an ASP.NET Core application, but they serve different purposes. We look at both of them in separate sections.

WebApplicationBuilder

This class is typically used to setup the configuration for ASP.NET Core application. It provides a fluent API to configure services, middleware, and the application pipeline.

Key Methods and Properties:
  • Services: This property is used to access the IServiceCollection to register dependencies.
  • Configuration: This property is used to access the IConfiguration instance to configure app settings.
  • Host: This property is used to access IHostBuilder for configuring the host.
  • Build(): This method is used to create and configure WebApplication instance. 
  • Example:
      var builder = WebApplication.CreateBuilder(args);
      builder.Services.AddControllers();
      var app = builder.Build();

WebApplicationOptions

This class provides options to configure how the WebApplicationBuilder is set up. It is used to define settings related to the application's environment, such as the URLs to bind to, the content root, and other configuration settings before building the application. This is passed as an optional parameter to the WebApplication.CreateBuilder method to customize various aspects of the application such as content and web root paths, environment and application names etc. 

Key Properties:

  • Args: Command-line arguments passed to the application.
  • ApplicationName: The name of the application.
  • ContentRootPath: Path to the content root directory.
  • EnvironmentName: Specifies the environment (e.g., Development, Production).
  • WebRootPath: Path to the wwwroot folder for static files. Example:
var options = new WebApplicationOptions
{
    EnvironmentName = "Development",
    ContentRootPath = "/path/to/content"
};
var builder = WebApplication.CreateBuilder(options);

Key Difference

  • WebApplicationBuilder is the primary class to build and configure the application (services, middleware, etc.).
  • WebApplicationOptions is used to specify configuration settings that are applied when creating the WebApplicationBuilder. 

Summary

WebApplicationBuilder can be used to configure the application and set up the request pipeline, while WebApplicationOptions to customize the initial configuration of the builder.

What is the default name of ASP.NET Core web application?

In an ASP.NET Core web application, the default name of the application is derived from the project's assembly name. The assembly name typically matches the project name by default. This name is reflected in the ApplicationName property of the application configuration, which is part of the WebApplicationOptions.

It is also used in logging, configuration, and other application-level metadata. For example, if you create a project named MyApp, the default application name will be MyApp.

The the application name can be explicitly specified using the ApplicationName property of WebApplicationOptions or by configuring it in the Host settings:

var options = new WebApplicationOptions
{
    ApplicationName = "MyCustomApp"
};

var builder = WebApplication.CreateBuilder(options);

Otherwise, ASP.NET Core uses the project or assembly name as the default.

No comments:

Post a Comment

Hot Topics