Azure .NET Core Application Settings


Overview

This article outlines how to work with Application Settings in ASP.NET Core with Azure Web Apps (Azure App Services) super simple Jeff version…

How To

Ref: Working with Azure App Services Application Settings and Connection Strings in ASP.NET Core

The above article is wonderful but adds some complexity.  It also does not tell you how to get the Configuration in your controllers etc…  It is super simple to do this and here is a quick sample for you to follow.

The default WebApp templates provide the wiring for the Configuration like this:

        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

If it is not there, use a template or add it yourself by copying the code and dependencies from a blank project.

Now you need to add this to the services as a singleton with the line:  services.AddSingleton<IConfiguration>(Configuration);

// This method gets called by the runtime. Use this method to add services to the container.
       public void ConfigureServices(IServiceCollection services)
       {
           // Add framework services.
           services.AddMvc();
           // Add the configuration singleton here
           services.AddSingleton<IConfiguration>(Configuration);

       }

In the controller code, or page code, add a new constructor that takes IConfiguration.  This will ensure that value is passed.  Then you simply store this as a variable to use in your code:

    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        // Place to store the Config object and use in this controller
        private readonly IConfiguration Config;

        // Constructor that that takes IConfiguration is called on instantiation thanks to Dependency injection
        public ValuesController(IConfiguration config)
        {
            Config = config;
        }

        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            // Use the Config object set in the constructor
            var x = Config["mykey"];
            return new string[] { "value1", "value2" };
        }

Now you can set mykey in the portal and this value will be read.  You can also set it in the appsettings.json to set the value when testing local with IIS Express.

Note that Connection strings can be retrieved using something like: Config.GetConnectionString(“myconnstring”);

Drop me a note if you find this useful!

References

Working with Azure App Services Application Settings and Connection Strings in ASP.NET Core

Introduction to Dependency Injection in ASP.NET Core

Configuration in ASP.NET Core

 

key words: “.net core azure webapp” “.net core azure appsettings” “.net core CloudConfigurationManager”


Comments (0)

Skip to main content