web:asp.net:core:configuration
Ceci est une ancienne révision du document !
Configuration
Exemple simple
Dans le constructeur de Startup.cs
:
var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); this.Configuration = builder.Build();
Le fichier appsettings.json
:
{ // http://www.timhortons.com/ca/en/menu/donuts.php "Options": [ "option1", "option2", "option3" ] }
La classe MyOptions
:
public class MyOptions { public string[] Options { get; set; } }
Dans le fichier Startup.cs
, méthode ConfigureServices
:
MyOptions myOptions = new MyOptions(); services.Configure<MyOptions>(this.Configuration); this.Configuration.Bind(myOptions);
Autre exemple:
{ "Section": { "MyOptions": [ "option1", "option2" ] } }
MyOptions myOptions = new MyOptions(); this.Configuration.GetSection("Section").Bind(myOptions);
web/asp.net/core/configuration.1509557998.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)