Copied to clipboard

Flag this post as spam?

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


  • Darren Hunter 105 posts 196 karma points
    14 days ago
    Darren Hunter
    0

    Pulling values from appsettings.json in Block List and Grid

    Hi,

    I have got an issue in Umbraco 13 I can't for the life of me work out how to pull some custom settings I have setup in the appsettings.json file for goodle maps in to a Block Grid or Block List.

    In the version 8 site we use.

    var apiKey = System.Configuration.ConfigurationManager.AppSettings["googleAPIKey"];

    But this dose not work in version 13.

    Can any one help. I dont want to have to write my own JSON parser if I dont have to,

  • Steve Morgan 1346 posts 4453 karma points c-trib
    14 days ago
    Steve Morgan
    0

    Example is for a view / partial but same idea for controller / view component.

    Inject the configuration - so for a view:

    @inject IConfiguration _configuration

    (possibly need @inject Microsoft.Extensions.Configuration.IConfiguration _configuration)

    Then you can access values in the Json like this:

    <h2>Debug: @_configuration["Umbraco:Cms:Hosting:Debug"]</h2>
    

    Or if it's not a string - you can use get value to use type setting:

    <h2>Debug: @_configuration.GetValue<bool>("Umbraco:Cms:Hosting:Debug")</h2>
    
  • Darren Hunter 105 posts 196 karma points
    14 days ago
    Darren Hunter
    0

    Thanks you

    get the following code to work:

    @using Umbraco.Extensions @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage

    @{ var maplat = Model.Content.Value("latitude").ToString(); var maplng = Model.Content.Value("longitude").ToString(); var apikey = _configuration["CustomSettings:googleAPIKey"];

    string s = "";
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft