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.

About the author

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.

Related Posts

How to use Donut Caching in Umbraco and MVC

In this post I show you how easy Donut Caching is to use in Umbraco and MVC. It's very clever and no…

Read Post

How to start working with Umbraco v8

In this post I give you step by step instructions for getting Umbraco v8 forked, checked out and wor…

Read Post

How to add default values to Umbraco properties in the backoffice

This post shows you how you can easily add a default value to a property when using the editor in th…

Read Post

How to solve Multiple types were found that match the controller named x

This post shows you how to solve the error 'Multiple types were found that match the controller name…

Read Post