2

I really enjoyed the new Configuration feature of Asp.Net vNext using de default appsettings.json

But I would like to change the values of that file when I publish the website as a Azure Web App.

The old web.config appsettings was easy to change and configure the properties on the environment.

Do you know how to do this? I prefer to user the default provider instead of creating a custom configuration provider.

Thank you!

Murilo Maciel Curti
  • 2,677
  • 1
  • 21
  • 26

2 Answers2

6

If you set App Settings in the Azure Portal, they become environment variables at runtime, and should get picked up by the ASP.NET vNext runtime. So you don't need to physically modify your appsettings.json to achieve this.

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Can you please post an example on how to fetch the environment data? – shlatchz Jun 06 '16 at 15:47
  • See http://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c – David Ebbo Jun 06 '16 at 16:09
  • I added env variables to the configbuilder `configurationBuilder.AddEnvironmentVariables()` and then used `Configuration["APPSETTING_EnvVarName"]` for an environment variable that I saved at the Azure Web App's app settings in the portal. – shlatchz Jun 08 '16 at 11:59
1

It worked very well David! Thank you!

Here a sample to help our friends with the same question:

startup.cs

public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsDevelopment())
        {
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            builder.AddUserSecrets();
        }

        **builder.AddEnvironmentVariables();**
        Configuration = builder.Build();
    }

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc();

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();

        **services.AddInstance<IConfiguration>(Configuration);**
    }

HomeController.cs

IConfiguration _configuration;

    public HomeController(IConfiguration configuration)
    {
        this._configuration = configuration;

    }
    public IActionResult Index()
    {

        ViewBag.key = _configuration["Data:DefaultConnection:ConnectionString"];
        return View();
    }

Index.cshtml

@{
ViewData["Title"] = "Home Page";

} @ViewBag.key

To see the difference, run the web app on localhost and on an azure web app changing the appsetting Data:DefaultConnection:ConnectionString

Best,

Murilo Maciel Curti
  • 2,677
  • 1
  • 21
  • 26