Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make app settings in .NET Core that translate to Azure app settings?

I can't remember where I saw this but I followed the advice on a blog when setting up my app configuration for my .NET Core MVC application. I created a model like this to hold some settings my app needed:

public class BasePathSettings
{
    public string BaseImageFolder { get; set; }
    public string BaseApiUrl { get; set; }
}

My StartUp has this...

    public void ConfigureServices(IServiceCollection services)
    {
        ... 
        // this adds the base paths to container
        services.Configure<BasePathSettings>(Configuration.GetSection("BasePathSettings"));

        ....
    }

And the appsettings.json has this in it:

"BasePathSettings": {
  "BaseImageFolder": "D:\\Images\\",
  "BaseApiUrl": "http://localhost:50321/"
},

I inject the controllers that need this info like so....

    private readonly BasePathSettings _settings;

    public ClientsController(IOptions<BasePathSettings> settings)
    {
        _settings = settings.Value;

        _client = new HttpClient();
        _client.BaseAddress = new Uri(_settings.BaseApiUrl);
    }

Running this on my localhost everything runs fine.

However, when I deploy this application to Azure I assumed I needed to create an application setting in the General Settings of the app service. So I made an app setting called BasePathSettings and copied the json for the setting into the value:

 { "BaseImageFolder": "imagePath", "BaseApiUrl": "apiUrl" } 

It appears that Azure barfs when it's in the ConfigureServices code claiming that the web.config does not have the correct permissions in NTFS. I'm guessing the real culprit is how the json value is being read from the Azure application settings.

Can I even use json there? If so, does it need formatted differently?

like image 888
Sailing Judo Avatar asked Mar 16 '17 05:03

Sailing Judo


People also ask

How do I read app settings from Azure in .NET core?

In the Azure portal, search for and select App Services, and then select your app. In the app's left menu, select Configuration > Application settings. For ASP.NET and ASP.NET Core developers, setting connection strings in App Service are like setting them in <connectionStrings> in Web.

Can I use app config in .net core?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.


1 Answers

Can I even use json there? If so, does it need formatted differently?

To add hierarchical structure settings to Azure web app, we could place a colon between the section name and the key name. For example,

use BasePathSettings:BaseImageFolder to set your folder
use BasePathSettings:BaseApiUrl to set your url
like image 197
Amor Avatar answered Nov 15 '22 06:11

Amor