Objectives:
- Look at WebApplication Create and CreateBuilder methods of WebApplication
- Understanding use of WebApplication in ASP.NET Core
- Setup web application in ASP.NET Core
- It will help you to convert console app into ASP.NET Core
In ASP.NET Core, the WebApplication class provides several overloads for the Create and CreateBuilder methods, which are designed to initialize and configure an ASP.NET Core application in different ways, depending on the application's needs.
Here’s an explanation of the different overloads for each:
1. WebApplication.CreateBuilder() Overloads
The CreateBuilder method is used to initialize a WebApplicationBuilder, which helps configure the services and middleware pipeline before building the actual WebApplication.
Overloads:
- WebApplication.CreateBuilder()
- WebApplication.CreateBuilder(string[] args)
- WebApplication.CreateBuilder(WebApplicationOptions options)
Now look at each method separately.
- WebApplication.CreateBuilder(): It initializes the WebApplicationBuilder without passing any command-line arguments. This overload is often used when you don't need to access or use command-line arguments during app configuration.
- Usage:
var builder = WebApplication.CreateBuilder();
var builder = WebApplication.CreateBuilder(args);
var options = new WebApplicationOptions
{
ContentRootPath = "/my/custom/path",
ApplicationName = "MyApp"
};
var builder = WebApplication.CreateBuilder(options);
var options = new WebApplicationOptions
{
EnvironmentName = "Development"
};
var builder = WebApplication.CreateBuilder(options, args);
WebApplication.Create() Overloads
The Create method builds the WebApplication from an already configured IHostBuilder. It is typically used when you need more control over the host setup than what CreateBuilder() provides. Overloads:- WebApplication.Create(): This method doesn't take any arguments. It creates a basic WebApplication instance by setting up default services, configuration, and the request pipeline.
- Usage:
var app = WebApplication.Create();
var hostBuilder = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
var app = WebApplication.Create(hostBuilder);
Summary of Overloads
CreateBuilder() Overloads:- CreateBuilder(): It initializes the WebApplicationBuilder with no or minimal configuration.
- CreateBuilder(string[] args): It allows passing command-line arguments to influence configuration.
- CreateBuilder(WebApplicationOptions options): It gives you more control over app configuration through options like content root and environment.
- CreateBuilder(WebApplicationOptions options, string[] args): It combines options and command-line arguments.
- Create(): It sets up a basic WebApplication.
- Create(IHostBuilder hostBuilder): It allows you to use a custom host builder for more advanced configuration. The choice of overload depends on whether you need to pass arguments, configure app settings, or require advanced control over hosting and configuration.
No comments:
Post a Comment