This post gives you some examples of how you can check for errors when retrieving web.config app setting values.

If you have a value stored in your web config app settings, you would normally access them in the following way:

<appSettings>
<add key="AdminEmail" value="[email protected]"/> <add key="MinimumQuantity" value="5"/> <add key="ConversionRate" value="1.25"/> </appSettings>

string adminEmail = System.Web.Configuration.WebConfigurationManager.AppSettings["AdminEmail"];

You may want to add logic around the values, but if you are doing this everywhere in your code that you need to use an app setting value, you will find a lot of duplicated code and if you don't add any logic around it, you may experience errors

Here are some helper methods for you to use, and examples of how you would call them:

using System;
using System.Web.Configuration;

namespace CodeShare.Utility
{
public static class Helper
{
const string APP_SETTING_ERROR_MESSAGE = "Invalid or missing appSetting, ";

public static string GetStringFromAppSetting(string appSettingName)
{
if (WebConfigurationManager.AppSettings[appSettingName] != null && !String.IsNullOrEmpty(WebConfigurationManager.AppSettings[appSettingName].ToString()))
{
return WebConfigurationManager.AppSettings[appSettingName].ToString();
}
else
{
throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
}
}

public static double GetDoubleFromAppSetting(string appSettingName)
{
double doubleValue = 0;
if (WebConfigurationManager.AppSettings[appSettingName] == null || !double.TryParse(WebConfigurationManager.AppSettings[appSettingName].ToString(), out doubleValue))
{
throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
}
return doubleValue;
}

public static int GetIntFromAppSetting(string appSettingName)
{
int intValue = 0;
if (WebConfigurationManager.AppSettings[appSettingName] == null || !int.TryParse(WebConfigurationManager.AppSettings[appSettingName].ToString(), out intValue))
{
throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
}
return intValue;
}
}
}

using CodeShare.Utility;

string adminEmail = GetStringFromAppSetting("AdminEmail");

double conversionRate = GetDoubleFromAppSetting("ConversionRate");

int minimumQuantity = GetIntFromAppSetting("MinimumQuantity");

Now when you run your code, if any of the app setting values are missing or invalid, it will throw an error telling you the name of the app setting that is invalid or missing.

UPDATE

I shared this post on LinkedIn and had some great input from other developers, so following their feedback, I have update the code. It is more concise, and puts the app settings into a single static class, meaning you only enter the name of the app setting once, avoiding using string literals in your code more than you need to.

using System;
using System.Web.Configuration;

namespace CodeShare.Utility
{
public static class ConfigSettings
{

private const string APP_SETTING_ERROR_MESSAGE = "Invalid or missing appSetting, ";

public static string AdminEmail { get { return GetStringFromAppSetting("AdminEmail"); } }
public static double ConversionRate { get { return GetDoubleFromAppSetting("ConversionRate"); } }
public static int MinimumQuantity { get { return GetIntFromAppSetting("MinimumQuantity"); } }


public static string GetStringFromAppSetting(string appSettingName)
{
string setting = WebConfigurationManager.AppSettings[appSettingName] as string;
if (String.IsNullOrEmpty(setting))
{
throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
}
return setting;
}

public static double GetDoubleFromAppSetting(string appSettingName)
{
double doubleValue = 0;
string setting = GetStringFromAppSetting(appSettingName);
if (!double.TryParse(setting, out doubleValue))
{
throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
}
return doubleValue;
}

public static int GetIntFromAppSetting(string appSettingName)
{
int intValue = 0;
string setting = GetStringFromAppSetting(appSettingName);
if (!int.TryParse(setting, out intValue))
{
throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
}
return intValue;
}
}
}

//Here is the new usage example
using CodeShare.Utility;

public class UsageExample
{
private void UsingTheCode()
{
string adminEmail = ConfigSettings.AdminEmail;
double conversionRate = ConfigSettings.ConversionRate;
int minimumQuantity = ConfigSettings.MinimumQuantity;
}
}

Thanks Robbert and James who helped me in this post on LinkedIn

 

I hope this post was useful to some of you. I apologise if you already knew this. If you have a better way of doing it, please feel free to add it in the comments for others to see.

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.