22

I want to get a logFilePath value which I gave by hardcode in to appSettings. I am trying to reach the key value by

System.Configuration.Configuration rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
System.Configuration.KeyValueConfigurationElement customSetting = rootWebConfig1.AppSettings.Settings["azureLogUrl"];
string pathValue =  customSetting.Value;

but I am getting null reference exception . How can I get the value from web.config file?

2 Answers 2

61

Use:

string pathValue = ConfigurationManager.AppSettings["azureLogUrl"];

You do not need to cast this to string and check for nulls because documentation states:

Values read from the appSettings element of the Web.config file are always of type String. If the specified key does not exist in the Web.config file, no error occurs. Instead, an empty string is returned.

Sign up to request clarification or add additional context in comments.

1 Comment

need to add using System.Configuration;
5

You can get the value from web.config like this :

string pathValue = WebConfigurationManager.AppSettings["azureLogUrl"].ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.