Skip to content

Commit

Permalink
docs: Create sections with the .NET configuration system (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed Jun 25, 2024
1 parent fd4f77e commit d7667a2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion samples/DotEnv.Example.ConfigurationApi/.env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
APP_BASE_URL=https://github.com/MrDave1999/dotenv.core
Settings__APP_BASE_URL=https://github.com/MrDave1999/dotenv.core
Settings__SERVER=example.com
Settings__DATABASE=Northwind
17 changes: 15 additions & 2 deletions samples/DotEnv.Example.ConfigurationApi/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ public HomeController(IConfiguration configuration)
}

[HttpGet]
public string Get()
public SettingsDto Get()
{
return _configuration["APP_BASE_URL"];
var section = _configuration.GetSection("Settings");
return new()
{
AppBaseUrl = section["APP_BASE_URL"],
Server = section["SERVER"],
Database = section["DATABASE"]
};
}

public class SettingsDto
{
public string AppBaseUrl { get; init; }
public string Server { get; init; }
public string Database { get; init; }
}
}
4 changes: 3 additions & 1 deletion samples/DotEnv.Example.OptionsPattern/.env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
AppBaseUrl=https://github.com/MrDave1999/dotenv.core
Settings__AppBaseUrl=https://github.com/MrDave1999/dotenv.core
Settings__Server=example.com
Settings__Database=Northwind
2 changes: 2 additions & 0 deletions samples/DotEnv.Example.OptionsPattern/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public class AppSettings
{
public string AppBaseUrl { get; init; }
public string Server { get; init; }
public string Database { get; init; }
}
4 changes: 2 additions & 2 deletions samples/DotEnv.Example.OptionsPattern/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public HomeController(IOptions<AppSettings> options)
}

[HttpGet]
public string Get()
public AppSettings Get()
{
return _appSettings.AppBaseUrl;
return _appSettings;
}
}
2 changes: 1 addition & 1 deletion samples/DotEnv.Example.OptionsPattern/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// Adds environment variables in the configuration collection.
builder.Configuration.AddEnvironmentVariables();
// Registers a configuration instance which AppSettings will bind against.
builder.Services.Configure<AppSettings>(builder.Configuration);
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("Settings"));

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
Expand Down

0 comments on commit d7667a2

Please sign in to comment.