WebConfigHelper 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package WebConfigHelper --version 1.0.3
NuGet\Install-Package WebConfigHelper -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WebConfigHelper" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WebConfigHelper --version 1.0.3
#r "nuget: WebConfigHelper, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WebConfigHelper as a Cake Addin
#addin nuget:?package=WebConfigHelper&version=1.0.3

// Install WebConfigHelper as a Cake Tool
#tool nuget:?package=WebConfigHelper&version=1.0.3

Web.Config AppSettings Helper

DepShield Badge Build status

This component allows you to get strongly typed appsetting values from the web.config file.

Example web.config app settings

<appSettings>
    <add key="appVersion" value="15" />    
    <add key="releaseDate" value="01/02/2019" />
    <add key="appName" value="TheGreatestApp" />
    <add key="versions" value="1, 9, 15, 23" />
    <add key="keyDates" value="01/02/2019, 01/03/2019, 01/04/2019" />
    <add key="names" value="Fred, Sarah, Sam" />
</appSettings>

Getting a setting:

var config = new WebConfigValues();

var appVersion = config.GetAppSetting<int>("appVersion");
var releaseDate = config.GetAppSetting<DateTime>("releaseDate");
var appName = config.GetAppSetting<string>("appName");

// Return a comma separated list as an array
var versions = config.GetAppSettingArray<int>("versions");
var keyDates = config.GetAppSettingArray<DateTime>("keyDates");
var names = config.GetAppSettingArray<string>("names");

// with a default value
var appVersion = config.GetAppSetting("appVersion", 1);
var releaseDate = config.GetAppSettingArray<DateTime>("releaseDate", DateTime.Parse("01/01/2000");
var versions = config.GetAppSettingArray("versions", new int[] { 1, 2 });
var keyDates = config.GetAppSettingArray("keyDates", new int[] {  DateTime.Parse("01/01/2000"),  DateTime.Parse("01/01/2001") });

Handling null values

// Either use a default value
var timeout = config.GetAppSetting("timeout", 30);

// or use a nullable type
var timeout = config.GetAppSetting<int?>("timeout");

// otherwise it will fail
var timeout = config.GetAppSetting<int>("timeout");
// System.ArgumentNullException : Setting 'timeout' returned null and type System.Int32 cannot have a null value

Setting up a unit test (using Moq)

When you are creating unit tests for your web application, you can mock IWebConfigProvider allowing you to test different app settings values.

var provider = new Mock<IWebConfigProvider>();
// GetAppSetting always returns a string value
provider.Setup(x => x.GetAppSetting("appVersion")).Returns("1");
provider.Setup(x => x.GetAppSetting(versions)).Returns("1, 9, 15, 23");

var config = new WebConfigValues(provider.Object);
var appVersion = config.GetAppSetting<int>("appVersion");
var versions = config.GetAppSettingArray<int>("versions");
Product Compatible and additional computed target framework versions.
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 2,070 2/28/2019
1.0.3 1,021 2/4/2019
1.0.0-beta 528 2/1/2019