1、startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.Configure<MyOptions>(Configuration);
}
2、controller
private readonly MyOptions _options;
public HomeController(IOptions<MyOptions> optionsAccessor)
{
_options = optionsAccessor.Value;
}
public class MyOptions
{
public string name { get; set; }
public string name2 { get; set; }
}
类MyOptions里的信息对应appsettings.json里的值。
获取信息只要使用“_options.属性”。