Asp net core access configuration from static class

Introduction

ASP.NET is a popular programming language used for building web . One common requirement in web development is the need to access configuration settings from a static . In this article, we will explore how to achieve this in ASP.NET Core.

Accessing Configuration Settings

ASP.NET Core provides a flexible and powerful configuration system that allows to easily access configuration settings from various such as .json, environment , command-line arguments, and more. To access configuration settings from a static class, we can leverage the IConfiguration interface provided by ASP.NET Core.

First, let's start by adding the necessary dependencies to our project. Open the Startup.cs file and locate the method. Add the following code to configure the configuration settings:


public void ConfigureServices(IServiceCollection services)
{
    // Other configuration code...

    IConfiguration configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .();

    services.AddSingleton(configuration);
}

Here, we are creating a new instance of the ConfigurationBuilder class and adding the appsettings.json file as a configuration source. We then build the configuration and register it as a singleton service in the dependency injection container.

Accessing Configuration Settings in a Static Class

Now that we have registered the configuration settings, we can access them from a static class. Let's create a new static class called AppSettings and add a property to retrieve a specific configuration setting:


public static class AppSettings
{
    private static readonly IConfiguration _configuration;

    static AppSettings()
    {
        _configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .Build();
    }

    public static string GetSetting(string key)
    {
        return _configuration[key];
    }
}

In this example, we create a static constructor to initialize the _configuration field with the same configuration settings used in the Startup.cs file. We then expose a GetSetting method that retrieves a specific configuration setting based on the provided key.

Example

Now that we have our AppSettings class, we can use it to access configuration settings from in our application. Here's an example:


string connectionString = AppSettings.GetSetting("ConnectionStrings:DefaultConnection");

In this example, we use the GetSetting method to retrieve the value of the DefaultConnection configuration setting from the ConnectionStrings section in the appsettings.json file.

Conclusion

Accessing configuration settings from a static class in ASP.NET Core is a common requirement in web development. By leveraging the IConfiguration interface and the configuration system provided by ASP.NET Core, we can easily access configuration settings from a static class. This allows us to centralize the configuration logic and make it easily accessible throughout our application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents