An open API service indexing awesome lists of open source software.

https://github.com/programystic/webconfighelper

Helper methods to access the web config app settings
https://github.com/programystic/webconfighelper

appsettings csharp strongly-typed web-config

Last synced: about 1 year ago
JSON representation

Helper methods to access the web config app settings

Awesome Lists containing this project

README

          

# Web.Config AppSettings Helper

[![Build status](https://programystic.visualstudio.com/WebConfigHelper/_apis/build/status/WebConfigHelper-.NET%20Desktop-CI)](https://programystic.visualstudio.com/WebConfigHelper/_build/latest?definitionId=11)

[![NuGet package](https://img.shields.io/nuget/v/WebConfigHelper.svg)](https://nuget.org/packages/WebConfigHelper)

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

## Getting a setting:
```cs
var config = new WebConfigValues();

var appVersion = config.GetAppSetting("appVersion");
var releaseDate = config.GetAppSetting("releaseDate");
var appName = config.GetAppSetting("appName");

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

// with a default value
var appVersion = config.GetAppSetting("appVersion", 1);
var releaseDate = config.GetAppSettingArray("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
```cs
// Either use a default value
var timeout = config.GetAppSetting("timeout", 30);

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

// otherwise it will fail
var timeout = config.GetAppSetting("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.

```cs
var provider = new Mock();
// 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("appVersion");
var versions = config.GetAppSettingArray("versions");
```