Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Nov 04, 2022 @ 11:04
    Chriztian Steinmeier
    0

    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')

    How can I make this work?

    Thanks, /Chriztian

  • Matthew Care 12 posts 145 karma points c-trib
    Nov 04, 2022 @ 11:41
    Matthew Care
    1

    In your view you could do

    @inject IOptions<GlobalSettings> GlobalSettings

    and access the properties that way

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Nov 04, 2022 @ 12:11
    Chriztian Steinmeier
    0

    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

  • Matthew Care 12 posts 145 karma points c-trib
    Nov 04, 2022 @ 12:24
    Matthew Care
    100

    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).

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Nov 04, 2022 @ 12:35
    Chriztian Steinmeier
    1

    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

Please Sign in or register to post replies

Write your reply to:

Draft