Copied to clipboard

Flag this post as spam?

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


  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Jan 22, 2022 @ 06:16
    Mikael Axel Kleinwort
    0

    Reading strongly-typed values from umbracoSettings.config

    Hello,

    is there a way to access the settings information of Umbraco 8 umbracoSettings.config file in a strongly-typed way?

    For the task at hands, I need to get to the imaging->imageFileTypes value.

    Kind regards! Mikael

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Jan 22, 2022 @ 18:23
    Anders Bjerner
    0

    Hi Mikael

    In Umbraco 8 can inject the IUmbracoSettingsSection type via dependency injection:

    using Umbraco.Core.Configuration.UmbracoSettings;
    
    public class MyClass {
    
        private readonly IUmbracoSettingsSection _umbracoSettings;
    
        public MyClass(IUmbracoSettingsSection umbracoSettings) {
            _umbracoSettings = umbracoSettings;
        }
    
    }
    

    Or alternatively via Current:

    @using Umbraco.Core.Composing
    @using Umbraco.Core.Configuration.UmbracoSettings
    
    @{
    
        IUmbracoSettingsSection umbracoSettings1 = Current.Configs.Settings();
        IUmbracoSettingsSection umbracoSettings2 = Current.Factory.GetInstance<IUmbracoSettingsSection>();
    
        IEnumerable<string> allowedTypes = umbracoSettings1.Content.ImageFileTypes;
    
        <h5>Allowed types:</h5>
        <pre>@string.Join(", ", allowedTypes)</pre>
    
    }
    

    Also if you're looking to do the same in Umbraco 9, it can be done as:

    @using Microsoft.Extensions.Options
    @using Umbraco.Cms.Core.Configuration.Models
    @inject IOptions<ContentSettings> _contentSettings
    
    @{
    
        string[] allowedTypes = _contentSettings.Value.Imaging.ImageFileTypes;
    
        <h5>Allowed types:</h5>
        <pre>@string.Join("\r\n", allowedTypes)</pre>
    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft