Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

ConfigManager - ASP.NET and Azure Functions

RSS
Modified on Wed, May 13, 2020, 9:19 AM by Administrator Categorized as ASP·NET MVC, ·Net Framework

Overview

The following class will work both in the context of ASP.NET and an Azure Function application.

Code

public static class ConfigManager
{
    public static string GetSetting(string name)
    {
        var result = Environment.GetEnvironmentVariable(name);

        if (string.IsNullOrWhiteSpace(result))
        {
            result = ConfigurationManager.AppSettings[name];
        }

        return result;
    }

    public static int? GetIntSetting(string name)
    {
        var s = GetSetting(name);

        if (int.TryParse(s, out int result))
            return result;

        return null;
    }

    public static DateTime? GetDateSetting(string name)
    {
        var s = GetSetting(name);

        if (DateTime.TryParse(s, out DateTime result))
            return result;

        return null;
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.