Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference an appSetting in a different part of web.config

I have my appSettings defined in a separate config file called Appsettings.Dev.Config, and I include that file inside my web.config file like so

<appSettings configSource="ConfigFiles\AppSettings.Dev.config"/>

Lets say one of the settings in the file is

<add key="MailerEmailAccount" value="[email protected]" />

Can I access the value of the setting MailerEmailAccount elsewhere inside web.config? How?

like image 763
floatingfrisbee Avatar asked Oct 06 '11 20:10

floatingfrisbee


People also ask

What is AppSetting section in web config?

The <appSettings> element stores custom application configuration information, such as database connection strings, file paths, XML Web service URLs, or any other custom configuration information for an application.

How do I use different web config files?

config (or any file) when you press F5 in Visual Studio. You can have different transformations based on the build configuration. This will enable you to easily have different app settings, connection strings, etc for Debug versus Release. If you want to transform other files you can do that too.

Where do I put appSettings in web config?

Locate the web. config file in the root directory of your application (or create one if it does not already exist). Add an <appSettings> element. Add <add> child elements along with key / value pairs to the <appSettings> element as required.

Where are connectionStrings in web config?

Connection strings go inside a <connectionStrings> element.


2 Answers

Nope, the web configuration file cannot pull "settings" from itself; it's not dynamic at all. The only sort of dynamic functionality is the ability to include other .config, but that's just a "suck all these settings in as if they were part of me" kind of thing.

like image 111
CodingGorilla Avatar answered Sep 22 '22 16:09

CodingGorilla


It might be possible if you create a custom ConfigurationSection that pulls the value from appSettings.

Here's an article that explain how to create a custom configuration section:
http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx

I don't know if this is what you're looking for, but it's the only way I can think of to read a web.config setting from within the web.config.

EDIT

I haven't tested this, but maybe something like this would work?:

[ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
public string LocalName
{
    get
    {
        return this["localName"] as string;
    }
    set
    {                
        this["localName"] = WebConfigurationManager.AppSettings.Get(value);
    }
}
like image 20
James Johnson Avatar answered Sep 21 '22 16:09

James Johnson