I'm totally NEW into developing web applications with ASP.NET 8 and Razor and I have spent several hours trying to port my Windows App to web. I simply cannot wrap my head around how to make something very simple work:
In my app, I have a appSetting class that is read from a configuration file. This setting it used all over my application. Now I want something similar for my web app, but I keep running into all sorts of things I do not get.
I have made the class 'appSetting' and added it as a singleton in Program.cs:
builder.Services.AddSingleton<AppSetting>();
Then I think that I need to initialize it somewhere, and I have tried to do this in the 'index.cshtml.cs' file.
private readonly AppSetting _appSetting;
public string appVersion => _appSetting.version;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
_appSetting = new AppSetting();
}
In the constructor of my class, I try to read the cookie, but this just throws a runtime error: System.NullReferenceException: 'Object reference not set to an instance Of an object.'
public AppSetting()
{
var cookies = Request.Cookies["version"];
}
I simply do not know how to achieve what I want.
Thanks in advance
Lars Bo