I'm deploying our site to Umbraco Cloud, and I need the Development and the Live environment to hit different URLs for the RESTful APIs the site requires. Ideally I'd like to add the configs to appsettings.Development.json and appsettings.Production.json, but I'm unsure how to use that config directly in the cshtml files to pass the URLs into the DOM or set them in a javascript file in /scripts.
Thanks for the reply, Dennis, but that approach doesn't seem to work in Umbraco 11. Microsoft.Extensions.Configuration doesn't define a Configuration object, and System.Configuration doesn't have a static version of GetSection().
All the other docs I've seen talk about passing configuration via DI, but that's not going to work with .cshtml files of course.
Using Microsoft.Extensions.Configuration I can create an instance like:
var configuration = new ConfigurationBuilder().Build();
var myValue = configuration.GetValue<string>("myKey");
But that builds an empty configuration. In other dotNet Core apps I'd grab the config from the builder object returned from "WebApplication.CreateBuilder(args)" call in Program.cs, but that's only going to work if I'm writing my own controller or something.
Are there any examples out there on how to get the config with Umbraco 9+?
After spending the entire day reading through all the Umbraco documentation I could find and finding absolutely nothing on the topic, I finally jumped over to reading through Razor docs and found the answer.
appsettings and cshtml
I'm deploying our site to Umbraco Cloud, and I need the Development and the Live environment to hit different URLs for the RESTful APIs the site requires. Ideally I'd like to add the configs to appsettings.Development.json and appsettings.Production.json, but I'm unsure how to use that config directly in the cshtml files to pass the URLs into the DOM or set them in a javascript file in /scripts.
Umbraco 11.4.2 for the deploy.
Thanks for any and all help!
Hi Michael.
You can use appsettings.Development.json and appsettings.Production.json etc, and in the top of your cshtml view you should be able go use:
And then to use this variable in JS:
Hope that will work for you!
Thanks for the reply, Dennis, but that approach doesn't seem to work in Umbraco 11. Microsoft.Extensions.Configuration doesn't define a Configuration object, and System.Configuration doesn't have a static version of GetSection().
All the other docs I've seen talk about passing configuration via DI, but that's not going to work with .cshtml files of course.
Using Microsoft.Extensions.Configuration I can create an instance like:
But that builds an empty configuration. In other dotNet Core apps I'd grab the config from the builder object returned from "WebApplication.CreateBuilder(args)" call in Program.cs, but that's only going to work if I'm writing my own controller or something.
Are there any examples out there on how to get the config with Umbraco 9+?
Thanks!
After spending the entire day reading through all the Umbraco documentation I could find and finding absolutely nothing on the topic, I finally jumped over to reading through Razor docs and found the answer.
At the top of the view, we need to add:
Then in the body of the View in the C# sections we can look up values in appsettings like:
Jeeze this wasted a bunch of time figuring this out... :-(
Bravo @Michael Duffy
is working on a reply...