Overview : win.config 와 같은 기능
1.appsettings.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{ "Position": { "Title": "Editor", "Name": "Joe Smith" }, "MyKey": "My appsettings.json Value", "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" } |
2. C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class TestModel : PageModel { // requires using Microsoft.Extensions.Configuration; private readonly IConfiguration Configuration; public TestModel(IConfiguration configuration) { Configuration = configuration; } public ContentResult OnGet() { var myKeyValue = Configuration["MyKey"]; var title = Configuration["Position:Title"]; var name = Configuration["Position:Name"]; var defaultLogLevel = Configuration["Logging:LogLevel:Default"]; return Content($"MyKey value: {myKeyValue} \n" + $"Title: {title} \n" + $"Name: {name} \n" + $"Default Log Level: {defaultLogLevel}"); } } |