How to access configuration values from Helper file?
I have a file called Helpers.cs in App_Code which as a set of helper methods used in various views. I'd like to be able to access some of the global configuration keys, so I found that I had to do something like this:
namespace OurSite;
using Umbraco.Cms.Core.Configuration.Models;
using Microsoft.Extensions.Options;
public class Helpers {
private GlobalSettings _globalSettings;
public Helpers(IOptions<GlobalSettings> globalSettings) {
_globalSettings = globalSettings.Value;
}
public static string GetConfigValue() {
return _globalSettings.OurSite.ConfigurationData;
}
}
If I wasn't using the configuration stuff (e.g. if the method just returned a literal string) I could use it like this in a view:
@using OurSite;
@{
var config = Helpers.GetConfigValue();
}
But I'm getting an error (I assume) because of the static keyword when adding the configuration bits...
(The error is error CS0120: An object reference is required for the non-static field, method, or property 'Helpers._globalSettings')
Thanks Matthew 👍 - I did look at that option, but it means I'll have to change all the views that currently use the Helpers method, which is of course not ideal...
Was really hoping for someone to tell me what I'm doing "wrong" in trying to access the configuration settings.
Well, as you suspected you can't use dependency injection like that.
What you're doing "wrong" is trying to access a non-static value in a static method. Injected values are inherently not static, so you won't be able to access them like that.
It's not ideal, but if you really have no other way, you can use something like
Thanks a lot Matthew - That actually makes sense 🙌
I really struggle to understand Dependency Injection in "real code" - I can understand all the examples I find, but as soon as I need to do it with actual code, I seem to get nowhere - just around in circles where every new object needs another reference to exist :-)
How to access configuration values from Helper file?
I have a file called Helpers.cs in App_Code which as a set of helper methods used in various views. I'd like to be able to access some of the global configuration keys, so I found that I had to do something like this:
If I wasn't using the configuration stuff (e.g. if the method just returned a literal string) I could use it like this in a view:
But I'm getting an error (I assume) because of the
static
keyword when adding the configuration bits...(The error is
error CS0120: An object reference is required for the non-static field, method, or property 'Helpers._globalSettings'
)How can I make this work?
Thanks, /Chriztian
In your view you could do
@inject IOptions<GlobalSettings> GlobalSettings
and access the properties that way
Thanks Matthew 👍 - I did look at that option, but it means I'll have to change all the views that currently use the Helpers method, which is of course not ideal...
Was really hoping for someone to tell me what I'm doing "wrong" in trying to access the configuration settings.
/Chriztian
Well, as you suspected you can't use dependency injection like that.
What you're doing "wrong" is trying to access a non-static value in a static method. Injected values are inherently not static, so you won't be able to access them like that.
It's not ideal, but if you really have no other way, you can use something like
StaticServiceProvider.Instance.GetRequiredService<IOptions<GlobalSettings>>();
to ge the static instance of the settings. This should allow you to use your helper class like you want (untested).
Thanks a lot Matthew - That actually makes sense 🙌
I really struggle to understand Dependency Injection in "real code" - I can understand all the examples I find, but as soon as I need to do it with actual code, I seem to get nowhere - just around in circles where every new object needs another reference to exist :-)
Thanks again, /Chriztian
is working on a reply...