aspnetcore/fundamentals/minimal-apis/includes/webapplication7.md
:::moniker range="= aspnetcore-7.0"
WebApplicationThe following code is generated by an ASP.NET Core template:
The preceding code can be created via dotnet new web on the command line or selecting the Empty Web template in Visual Studio.
The following code creates a xref:Microsoft.AspNetCore.Builder.WebApplication (app) without explicitly creating a xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder:
WebApplication.Create initializes a new instance of the xref:Microsoft.AspNetCore.Builder.WebApplication class with preconfigured defaults.
[!INCLUDE webapplication7]
When a web app is created with Visual Studio or dotnet new, a Properties/launchSettings.json file is created that specifies the ports the app responds to. In the port setting samples that follow, running the app from Visual Studio returns an error dialog Unable to connect to web server 'AppName'. Visual Studio returns an error because it's expecting the port specified in Properties/launchSettings.json, but the app is using the port specified by app.Run("http://localhost:3000"). Run the following port changing samples from the command line.
The following sections set the port the app responds to.
In the preceding code, the app responds to port 3000.
In the following code, the app responds to port 3000 and 4000.
The following command makes the app respond to port 7777:
dotnet run --urls="https://localhost:7777"
If the Kestrel endpoint is also configured in the appsettings.json file, the appsettings.json file specified URL is used. For more information, see Kestrel endpoint configuration
The following code reads the port from the environment:
The preferred way to set the port from the environment is to use the ASPNETCORE_URLS environment variable, which is shown in the following section.
The ASPNETCORE_URLS environment variable is available to set the port:
ASPNETCORE_URLS=http://localhost:3000
ASPNETCORE_URLS supports multiple URLs:
ASPNETCORE_URLS=http://localhost:3000;https://localhost:5000
For more information using the environment, see xref:fundamentals/environments
The following samples demonstrate listening on all interfaces
http://0.0.0.0:3000The preceding samples can use ASPNETCORE_URLS
ASPNETCORE_URLS=http://*:3000;https://+:5000;http://0.0.0.0:5005
For more information on the development certificate, see Trust the ASP.NET Core HTTPS development certificate on Windows and macOS.
The following sections show how to specify the custom certificate using the appsettings.json file and via configuration.
appsettings.jsonThe following code reads from the configuration system:
For more information, see xref:fundamentals/configuration/index
The following code writes a message to the log on application startup:
For more information, see xref:fundamentals/logging/index
The following code shows how to get services from the DI container during application startup:
For more information, see xref:fundamentals/dependency-injection.
This section contains sample code using xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder.
The following code sets the content root, application name, and environment:
WebApplication.CreateBuilder initializes a new instance of the xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder class with preconfigured defaults.
For more information, see xref:fundamentals/index
The following table shows the environment variable and command-line argument used to change the content root, app name, and environment:
| feature | Environment variable | Command-line argument |
|---|---|---|
| Application name | ASPNETCORE_APPLICATIONNAME | --applicationName |
| Environment name | ASPNETCORE_ENVIRONMENT | --environment |
| Content root | ASPNETCORE_CONTENTROOT | --contentRoot |
The following sample adds the INI configuration provider:
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddIniFile("appsettings.ini");
var app = builder.Build();
For detailed information, see File configuration providers in xref:fundamentals/configuration/index.
By default the xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder reads configuration from multiple sources, including:
appSettings.json and appSettings.{environment}.jsonThe following code reads HelloKey from configuration and displays the value at the / endpoint. If the configuration value is null, "Hello" is assigned to message:
For a complete list of configuration sources read, see Default configuration in xref:fundamentals/configuration/index
Existing extension methods on xref:Microsoft.Extensions.Hosting.IHostBuilder can be accessed using the Host property:
Extension methods on xref:Microsoft.AspNetCore.Hosting.IWebHostBuilder can be accessed using the WebApplicationBuilder.WebHost property.
By default, the web root is relative to the content root in the wwwroot folder. Web root is where the Static File Middleware looks for static files. Web root can be changed with WebHostOptions, the command line, or with the xref:Microsoft.AspNetCore.Hosting.HostingAbstractionsWebHostBuilderExtensions.UseWebRoot%2A method:
The following example uses Autofac:
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
// Register services directly with Autofac here. Don't
// call builder.Populate(), that happens in AutofacServiceProviderFactory.
builder.Host.ConfigureContainer<ContainerBuilder>(builder => builder.RegisterModule(new MyApplicationModule()));
var app = builder.Build();
Any existing ASP.NET Core middleware can be configured on the WebApplication:
For more information, see xref:fundamentals/middleware/index
xref:Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder%2A?displayProperty=nameWithType initializes a new instance of the xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder class with preconfigured defaults. The developer exception page is enabled in the preconfigured defaults. When the following code is run in the development environment, navigating to / renders a friendly page that shows the exception.
:::moniker-end