Home > Net >  How do I access configuration options from within the app?
How do I access configuration options from within the app?

Time:11-03

What I tried:

HelloWorldController.cs

public class HelloWorldController : Controller
{
    public string Index()
    {
        StringBuilder output = new StringBuilder();

        // How do I do this?
        IConfiguration configuration = app.Configuration;
        List<String> list = configuration.options.ViewLocationExpanders;

        foreach(String item in list)
        {
            output.Append(HtmlEncoder.Default.Encode($"Looking in "   item));
        }

        return output.ToString();
    }
    ...
}

What I got: Will not compile

What I expected:

https://localhost:7127/HelloWorld

Looking in /Views/HelloWorld/Index.cshtml
Looking in /Views/Shared/Index.cshtml
Looking in /Pages/Shared/Index.cshtml

CodePudding user response:

You need to inject configuration into the constructor. The concept is called Dependency Injection (DI.)

All dependencies that the class needs are registered in startup.cs and then injected into the class through constructor.

[Route("")]
public class HomeController : Controller
{
    private Configuration _configuration;

    public HomeController(IOptions<Configuration> configuration)
    {
        _configuration = configuration.Value;
    }
    [HttpGet, Route("Healthcheck")]
    public string Healthcheck()
    {
        return "Website is up. Version: "   _configuration.IMAGE_TAG ?? "Not available";
    }

}

In startup.cs, this is how the Configuration class is registered.

    public IConfiguration Configuration { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        var connectionString = Configuration.GetValue<string>("DbEndpoints:DbConnectionString");

        services.Configure<Project.Server.Common.Configuration>(Configuration);
    }

Note that Project.Server.Common.Configuration is a regular class defined somewhere in the project that has the JSON configuration maps to (so it has the same structure as the appsettings JSON file.)

Example Configuration.cs

public class Configuration
{
    public string ASPNETCORE_ENVIRONMENT { get; set; }
    public string IMAGE_TAG { get; set; }
    public DatabaseEndpoint DbEndpoints { get; set; }
}

public class DatabaseEndpoint
{
    public string DbConnectionString { get; set; }
}

Note that you can also access environment variables like ASPNETCORE_ENVIRONMENT as long as you inject them as part of startup.cs constructor. (Note the AddEnvironmentVariables declaration below.

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

        Configuration = builder.Build();
    }
  • Related