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
- Host: GitHub
- URL: https://github.com/programystic/webconfighelper
- Owner: programystic
- License: mit
- Created: 2019-02-01T19:41:49.000Z (over 7 years ago)
- Default Branch: develop
- Last Pushed: 2019-03-01T13:45:40.000Z (over 7 years ago)
- Last Synced: 2025-03-06T21:16:16.965Z (over 1 year ago)
- Topics: appsettings, csharp, strongly-typed, web-config
- Language: C#
- Size: 1.71 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Web.Config AppSettings Helper
[](https://programystic.visualstudio.com/WebConfigHelper/_build/latest?definitionId=11)
[](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");
```