你如何在 web.config 中放置环境变量? [英] How do you put environmental variables in web.config?
问题描述
我目前 遵循这些教程,我想从 Azure 的 Web 应用程序应用程序设置中调用明文字符串.我的印象是环境变量是 用于非配置文件.但是,我想对 web.config 文件使用相同的方法.
I am currently Following these tutorials, and I am wanting to call the clear text string from Azure's Application Settings for Web Apps. I am under the impression that environmental variables are used for non-config files. However, I am wanting to use the same methodology for web.config files.
<connectionStrings configSource="/config/ConnectionStrings.config">
<add name="DefaultConnection" connectionString="@Environment.GetEnvironmentalVariable('SQLAZURECONNSTR_DefaultConnection')" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings file="configAppSettingsSecret.config">
<!-- Code Removed for Conciseness-->
<add key="mailAccount" value="@Environment.GetEnvironmentalVariable('APPSETTING_mailAccount')" />
<add key="mailPassword" value="@Environment.GetEnvironmentalVariable('APPSETTING_mailPassword')" />
<!-- Twilio-->
<add key="TwilioSid" value="@Environment.GetEnvironmentalVariable('APPSETTING_TwilioSid')" />
<add key="TwilioToken" value="@Environment.GetEnvironmentalVariable('APPSETTING_TwilioToken')" />
<add key="TwilioFromPhone" value="@Environment.GetEnvironmentalVariable('APPSETTING_TwilioFromPhone')" />
</appSettings>
注意:我包含了用于本地测试的 configSource="/example/".
Note: I included the configSource="/example/" for local testing.
推荐答案
对于应用程序,包括 Web 应用程序,在 Windows 上:
中的值只是字符串.如果您希望扩展环境变量,您的应用程序将需要自行完成.
For Applications, including Web Applications, On Windows:
The values in <appSettings>
are just strings. If you want environmental variables to be expanded your application will need to do that itself.
执行此操作的常用方法是使用 cmd
语法 %variable%
然后使用 Environment.ExpandEnvironmentVariables
来扩展它们.
A common way of doing this is to use the cmd
syntax %variable%
and then using Environment.ExpandEnvironmentVariables
to expand them.
规则不同(请参阅问题中的链接):但值似乎在环境变量中,因此,在配置文件中:
The rules are different (see links in the question): but the values appear to be in environment variables so, in the config file:
<add key='SomeSetting' value='%APPSETTING_some_key%'/>
然后检索:
var someSetting = Environment.ExpandEnvironmentVariables(
ConfigurationManager.AppSetting("SomeSetting"))
可能很好用.
这篇关于你如何在 web.config 中放置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!