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.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

Buy me a coffee

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 solve the error assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to set the default page base type to UmbracoViewPage in Umbraco

In this post I share with you what Ronald Barendse taught us about setting the pageBaseType in Umbra…

Read Post

How to fix corrupt accented characters in Excel for a C# export as CSV

This post will help you fix the issue where you csv export has corrupt characters when opening it in…

Read Post