There are no global variables in .NET, much less C#. Storing configuration data in global, static properties is a bad idea because it ties your code with the static class that holds those properties, making it harder to write or test code.
Configuration Middleware
ASP.NET Core solves this through the Configuration middleware which can read configuration settings from multiple providers, including files, databases and even in-memory collections. The Configuration overview article shows how to use a dictionary as a configuration source :
var builder = WebApplication.CreateBuilder(args);
var Dict = new Dictionary<string, string>
{
{"MyKey", "Dictionary MyKey Value"},
{"Position:Title", "Dictionary_Title"},
{"Position:Name", "Dictionary_Name" },
{"Logging:LogLevel:Default", "Warning"}
};
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
config.Sources.Clear();
config.AddInMemoryCollection(Dict);
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
});
builder.Services.AddRazorPages();
var app = builder.Build();
After that, all classes can retrieve the configuration values, no matter where they come from, through the IConfiguration
interface :
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}");
}
}
In your case you could load the settings from a database and register them as an in-memory source in your Program.cs
:
var Dict=LoadDataAsDictionary();
...
config.AddInMemoryCollection(Dict);
...
Dependency Injection Middleware
Another option is to load the data as a strongly typed object and register it as a Signleton instance using the Dependency Injection middleware :
var builder = WebApplication.CreateBuilder(args);
MyCacheData cacheData=LoadStronglyTypedData();
builder.Services.AddSingleton(cachedata);
This class can be injected into pages and controllers just like other DI services or IConfiguration
:
public class TestModel : PageModel
{
private readonly MyCacheData Data;
public TestModel(MyCacheData data)
{
Data = data;
}