Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add samples on how to create sections with the .NET configuration system #177

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading