Reading a Key from the Web.Config Using Configurationmanager

Reading a key from the Web.Config using ConfigurationManager

Try using the WebConfigurationManager class from the System.Web.Configuration namespace instead. For example:

string userName = WebConfigurationManager.AppSettings["PFUserName"]

Struggling to read value from web.config file using ConfigurationManager.AppSettings[key] in ASP .NET

Rename the web.config file to app.config and then it should work

Reading settings from app.config or web.config in .NET

You'll need to add a reference to System.Configuration in your project's references folder.

You should definitely be using the ConfigurationManager over the obsolete ConfigurationSettings.

Does ConfigurationManager.AppSettings[Key] read from the web.config file each time?

It gets cached, on first access of a property, so it does not read from the physical file each time you ask for a value. This is why it is necessary to restart an Windows app (or Refresh the config) to get the latest value, and why an ASP.Net app automatically restarts when you edit web.config. Why ASP.Net is hard wired to restart is discussed in the references in the answer How to prevent an ASP.NET application restarting when the web.config is modified.

We can verify this using ILSpy and looking at the internals of System.Configuration:

public static NameValueCollection AppSettings
{
get
{
object section = ConfigurationManager.GetSection("appSettings");
if (section == null || !(section is NameValueCollection))
{
throw new ConfigurationErrorsException(SR.GetString("Config_appsettings_declaration_invalid"));
}
return (NameValueCollection)section;
}
}

At first, this does indeed look like it will get the section every time. Looking at GetSection:

public static object GetSection(string sectionName)
{
if (string.IsNullOrEmpty(sectionName))
{
return null;
}
ConfigurationManager.PrepareConfigSystem();
return ConfigurationManager.s_configSystem.GetSection(sectionName);
}

The critical line here is the PrepareConfigSystem() method; this initializes an instance of the IInternalConfigSystem field held by the ConfigurationManager - the concrete type is ClientConfigurationSystem

As part of this load, an instance of the Configuration class is instantiated. This class is effectively an object representation of the config file, and appears to be held by the ClientConfigurationSystem's ClientConfigurationHost property in a static field - hence it is cached.

You could test this empirically by doing the following (in a Windows Form or WPF app):

  1. Starting your App up
  2. Access a value in app.config
  3. Make a change to app.config
  4. Check to see whether the new value is present
  5. Call ConfigurationManager.RefreshSection("appSettings")
  6. Check to see if the new value is present.

In fact, I could have saved myself some time if I'd just read the comment on the RefreshSection method :-)

/// <summary>Refreshes the named section so the next time that it is retrieved it will be re-read from disk.</summary>
/// <param name="sectionName">The configuration section name or the configuration path and section name of the section to refresh.</param>

Read value based on key from a Web.config section

Why You add this colors in webconfig? You can set these values in constant class . and you can get easily .

Please refer the below websites

Link 1

Link 2

But if you want webconfig,then please see this same discussions

Reading a key from the Web.Config using ConfigurationManager

How to read values from custom section in web.config

Issue reading AppSetting key from another project's web.config

worth to mention here

Web.config is part of another project and i am trying to access it
from another project,

That's your issue.

ConfigurationManager.AppSettings uses the configuration file tied to your entry assembly, you can't use one from a different project.

In this particular instance, your web.config should be a part of your project, in the root of the application

Access key value from Web.config in Razor View-MVC3 ASP.NET

@System.Configuration.ConfigurationManager.AppSettings["myKey"]

Using configurationmanager to read from multiple web.config files

You can read any config file with ease. Please see my sample code where I read application settings from external app.config file:

        System.Configuration.KeyValueConfigurationCollection settings;
System.Configuration.Configuration config;

System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
configFile.ExeConfigFilename = "my_file.config";
config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
settings = config.AppSettings.Settings;

Happy coding and best regards!

Can't read Web.config with ConfigurationManager.AppSettings

If you have a standard WCF-project, you should only have the Web.config-file, not App.config.

I would skip the old way of using appSettings altogether. Use applicationSettings instead, by using the Settings-tab in the project's properties.

It will create this in Web.Config:

<applicationSettings>
<Projectname.Properties.Settings>
<setting name="Testenvironment" serializeAs="String">
<value>True</value>
</setting>
</Projectname.Properties.Settings>
</applicationSettings>

For more information: appSettings vs applicationSettings. appSettings outdated?



Related Topics



Leave a reply



Submit