AspNetCore.Docs/aspnetcore/diagnostics/asp0013.md

2.0 KiB

title description author monikerRange ms.author ms.date uid
ASP0013: Suggest switching from using Configure methods to WebApplicationBuilder.Configuration Learn about analysis rule ASP0013: Suggest switching from using Configure methods to WebApplicationBuilder.Configuration rick-anderson >= aspnetcore-7.0 riande 09/27/2022 diagnostics/asp0013

ASP0013: Suggest switching from using Configure methods to WebApplicationBuilder.Configuration

Value
Rule ID ASP0013
Category Usage
Fix is breaking or non-breaking Non-breaking

Cause

xref:Microsoft.AspNetCore.Hosting.StartupBase.Configure%2A isn't the recommended strategy for reading and writing to configuration in a minimal API app. Configure was designed to be used with Web Host or .NET Generic Host. In a minimal API app, WebApplicationBuilder.Configuration should be used to modify configuration directly.

Rule description

Configure isn't the recommended strategy for configuring logging in a minimal API app.

var builder = WebApplication.CreateBuilder(args);

builder.Host.ConfigureAppConfiguration(builder =>
{
    builder.AddJsonFile("customAppSettings.json");
})

var app = builder.Build();

app.Run();

How to fix violations

To fix a violation of this rule, use WebApplicationBuilder.Configuration to modify application configuration directly without the need for an additional xref:Microsoft.Extensions.Hosting.HostBuilder.ConfigureAppConfiguration%2A call.

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile("customAppSettings.json");

var app = builder.Build();

app.Run();

When to suppress warnings

Do not suppress a warning from this rule.