feat(Konstanten): Erzeugen von Optionen aus Appsettings über IOptions.
- Konstantenmodell mit UserLanguages String-Array erstellen. Hinzufügen einer benutzerdefinierten Methode zum Abrufen der Eigenschaft über Name. - imlement in appsettings und confgire in Program.cs
This commit is contained in:
parent
84982e0286
commit
698151baf3
@ -0,0 +1,27 @@
|
||||
using DigitalData.UserManager.API.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.UserManager.API.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ConstantsController : ControllerBase
|
||||
{
|
||||
private readonly Constants _constants;
|
||||
public ConstantsController(IOptions<Constants> constantsOptions)
|
||||
{
|
||||
_constants = constantsOptions.Value;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetConstant(string? name = null)
|
||||
{
|
||||
if(name is null)
|
||||
return Ok(_constants);
|
||||
|
||||
|
||||
return Ok(_constants[name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
DigitalData.UserManager.API/Models/Constants.cs
Normal file
13
DigitalData.UserManager.API/Models/Constants.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace DigitalData.UserManager.API.Models
|
||||
{
|
||||
public class Constants
|
||||
{
|
||||
public IEnumerable<string> UserLanguages { get; init; } = Array.Empty<string>();
|
||||
|
||||
public object? this[string propertyName] => GetType()
|
||||
.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)?
|
||||
.GetValue(this, null);
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,8 @@ using DigitalData.Core.API;
|
||||
using DigitalData.UserManager.API.Controllers;
|
||||
using DigitalData.UserManager.Application.Services;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Reflection.Metadata;
|
||||
using DigitalData.UserManager.API.Models;
|
||||
|
||||
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
||||
logger.Debug("init main");
|
||||
@ -77,7 +79,9 @@ try {
|
||||
builder.Services.AddDirectorySearchService();
|
||||
|
||||
builder.Services.AddCookieBasedLocalizer();
|
||||
|
||||
|
||||
builder.ConfigureBySection<Constants>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
cnn_str = new(() =>
|
||||
|
||||
@ -73,5 +73,11 @@
|
||||
},
|
||||
// Delete below in production
|
||||
"UseEncryptor": true,
|
||||
"UseSwagger": true
|
||||
"UseSwagger": true,
|
||||
"Constants": {
|
||||
"UserLanguages": [
|
||||
"de-DE",
|
||||
"en-US"
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user