DenaliHardtail
DenaliHardtail

Reputation: 28316

What is the preferred method of handling missing or otherwise unavailable keys in a web.config

In my web.config, appSettings section, I have several keys. What is the preferred way to handle in code any keys that are missing? For example, a key name is changed due to a typo. I know I can use various methods to test if the values of said keys exist but what about the key itself?

Upvotes: 2

Views: 277

Answers (1)

Kieren Johnstone
Kieren Johnstone

Reputation: 42003

I found that ConfigurationManager.AppSettings["key"] (requires System.Configuration assembly reference) will return null if the key is not found.

In that case, I throw an instance of my own MyAppConfigurationException with something helpful: like Couldn't open repository, configuration key RepositoryAccess.Customer was not found.

Hope that helps!

Upvotes: 1

Related Questions