Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Umbraco 11.4.2 for the deploy.
Thanks for any and all help!
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:
var apiBaseUrl = Configuration.GetSection("ApiBaseUrl").Value;
And then to use this variable in JS:
var apiBaseUrl = @Html.Raw(Json.Serialize(apiBaseUrl));
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:
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.
At the top of the view, we need to add:
@inject IConfiguration Configuration
Then in the body of the View in the C# sections we can look up values in appsettings like:
var myValueOne= @Configuration["myTopLevelKey"];
var myValueTwo = @Configuration["mySection:myKey"];
Jeeze this wasted a bunch of time figuring this out... :-(
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted