你怎么把环境变量在web.config中? [英] How do you put environmental variables in web.config?

查看:663
本文介绍了你怎么把环境变量在web.config中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的以下这些教程的,而我想打电话从Web应用程序的Azure的应用程序设置清晰的文本字符串。我是IM pression下,环境变量是<一个href=\"http://azure.microsoft.com/blog/2013/07/17/windows-azure-web-sites-how-application-strings-and-connection-strings-work/\"相对=nofollow>用于非配置文件。然而,我想用相同的方法进行的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="config\AppSettingsSecret.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 =/例子/本地测试

Note: I included the configSource="/example/" for local testing.

推荐答案

&LT值;&的appSettings GT; 只是字符串。如果你想要的环境变量来扩大你的应用程序将需要做的本身。

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 语法%变量%,然后用<一个href=\"https://msdn.microsoft.com/en-us/library/system.environment.expandenvironmentvariables%28v=vs.110%29.aspx\"><$c$c>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"))

可以很好地工作。

may well work.

这篇关于你怎么把环境变量在web.config中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆