Copied to clipboard

Flag this post as spam?

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


  • Peter Laurie 42 posts 116 karma points
    Oct 08, 2021 @ 06:01
    Peter Laurie
    0

    Adding custom application specific settings to appSettings.json file

    Hi, I am trying to add custom settings to my new Umbraco 9 build but I have been unable to do so. I have tried adding my own appUmbracoSettings.json file but i cannot get any class or controller to read the values. In Our Umbraco 8 build I have just added my own values to the appSettings section of the web.config.

    On other .net core project that I have build, I have added an app settings section to the appsettings config file and used IConfiguration Microsoft.extensions.configuration to access the values, but when I try this on the Umbraco 9 install, all get is an error because the value is null. _configuration["AppSettings:EmailFrom"] for example, works perfectly well in my .net core worker services.

    Please can someone point me in the right direction, I thought the Umbraco 9 configuration section would have this information, but it does not. https://our.umbraco.com/documentation/Reference/V9-Config/

    Thank you

    Kind regards,

    Peter

  • Joao Pinto 13 posts 34 karma points
    Oct 12, 2021 @ 10:12
    Joao Pinto
    0

    Did you ever find a solution? I have the same issue. I need to add some additional stuff on appsettings.json but I don't know how this should be handled

  • Peter Laurie 42 posts 116 karma points
    Oct 12, 2021 @ 14:24
    Peter Laurie
    2

    Hi Joao, I found that within the Umbraco project I can add values to the appSettings.json: for example: "IgnoreNodeList": "1234" at the root level, say after the "Umbraco" section

    In the Controller (in this instance, it is the default controller), using the following dependency injection. I have done this before, in other .net worker services projects. You see I had some code for the Umbraco project I am testing with in another project, and tried this from there, and that is why it was not working. within the Umbraco project I tried the following:

    using Microsoft.Extensions.Configuration;
    
    public class DefaultController : RenderController { 
    private readonly IConfiguration Configuration;
    public DefaultController(ILogger<RenderController> logger,
    ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor,
    IConfiguration configuration, ISiteService siteService)
    : base(logger, compositeViewEngine, umbracoContextAccessor) {
    Configuration = configuration;
    }
    
    public override IActionResult Index() {
    
    string IgnoreNodeList = Configuration["IgnoreNodeList"]
    
    return CurrentTemplate(CurrentPage);
    }
    

    Kind regards,

    Peter

  • Joao Pinto 13 posts 34 karma points
    Oct 13, 2021 @ 08:30
    Joao Pinto
    1

    Hi Peter, thanks for your feedback, I tried a very similar approach, but on a class library instead of a controller and indeed it works. The whole thing is well explained on Microsoft's documentation here:

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0#appsettingsjson-1

Please Sign in or register to post replies

Write your reply to:

Draft