Copied to clipboard

Flag this post as spam?

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


  • bh 408 posts 1395 karma points
    Jun 29, 2022 @ 17:56
    bh
    0

    Appsettings.json in SurfaceController

    How do I access appsettings.json values in my SurfaceController?

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Jun 29, 2022 @ 18:40
  • bh 408 posts 1395 karma points
    Jun 29, 2022 @ 19:23
    bh
    0

    @HuwReddick thanks for the reply. Looks like that's for v8. When I attempted to apply that code to my SurfaceController in v9 IGlobalSettings doesn't exist.

    I tried injecting IConfiguration like this:

    public ContactFormController(
                IUmbracoContextAccessor umbracoContextAccessor,
                IUmbracoDatabaseFactory databaseFactory,
                ServiceContext services,
                AppCaches appCaches,
                IProfilingLogger profilingLogger,
                IPublishedUrlProvider publishedUrlProvider,
                IConfiguration configuration)
                : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider, configuration)
            {  }
    

    But I get this error:

    'SurfaceController' does not contain a constructor that takes 7 arguments
    
  • Johan Runsten 38 posts 276 karma points c-trib
    Jun 30, 2022 @ 06:21
    Johan Runsten
    100

    Hi!

    Just take an IConfiguration in your constructor, and don't pass it along to the base constructor:

        public ContactFormController(
                    IConfiguration configuration,
                    IUmbracoContextAccessor umbracoContextAccessor,
                    ...) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
    {
        this._configuration = configuration;
    }
    
  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Jun 30, 2022 @ 07:33
    Huw Reddick
    2

    As Johan stated, don't pass it to the base constructor, however I believe the correct DI would be something like

    IOptions<GlobalSettings> globalSettings
    

    GlobalSettings would be the section you want from appsettings.jsom

      "Umbraco": {
        "CMS": {
          "Content": {
            "MacroErrors": "Throw"
          },
          "Global": {
            "Smtp": {
              "From": "[email protected]",
              "Host": "localhost",
              "Port": 25
            }
          },
          "Hosting": {
            "Debug": true
          },
          "RuntimeMinification": {
            "useInMemoryCache": true,
            "cacheBuster": "Timestamp"
          }
        }
      }
    
  • bh 408 posts 1395 karma points
    Jun 30, 2022 @ 12:31
    bh
    0

    @HuwReddick thank you for this! I actually ended up using a combination of yours and @JohanRunsten solution. Thank you both for your help!!

    For anyone looking this helped with implementing the globalsettings: https://our.umbraco.com/documentation/Reference/Configuration/

  • Johan Runsten 38 posts 276 karma points c-trib
    Jun 30, 2022 @ 07:50
    Johan Runsten
    2

    That is a clean and strongly typed way of accessing a certain section. If you're using IConfiguration you can access the sections by configuration["Umbraco:CMS:Global"] but then you are dealing with string values.

Please Sign in or register to post replies

Write your reply to:

Draft