如何在web.config的不同部分引用appSetting

时间:2022-04-25 15:49:21

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定义在一个名为Appsettings.Dev.Config的单独配置文件中,我将该文件包含在我的web.config文件中,如此

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

Lets say one of the settings in the file is

让我们说文件中的一个设置是

<add key="MailerEmailAccount" value="myemail@myserver.com" />

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

我可以在web.config中的其他位置访问设置MailerEmailAccount的值吗?怎么样?

2 个解决方案

#1


4  

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.

不,Web配置文件无法从自身中提取“设置”;它根本不是动态的。唯一的动态功能就是能够包含其他.config,但这只是“将所有这些设置视为他们的一部分”。

#2


4  

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

如果您创建一个从appSettings中提取值的自定义ConfigurationSection,则可能是可能的。

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

这是一篇解释如何创建自定义配置部分的文章: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.

我不知道这是否是您正在寻找的,但这是我能想到从web.config中读取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);
    }
}

#1


4  

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.

不,Web配置文件无法从自身中提取“设置”;它根本不是动态的。唯一的动态功能就是能够包含其他.config,但这只是“将所有这些设置视为他们的一部分”。

#2


4  

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

如果您创建一个从appSettings中提取值的自定义ConfigurationSection,则可能是可能的。

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

这是一篇解释如何创建自定义配置部分的文章: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.

我不知道这是否是您正在寻找的,但这是我能想到从web.config中读取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);
    }
}