Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
788 views
in Technique[技术] by (71.8m points)

asp.net mvc - How do you put environmental variables in 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>

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

A common way of doing this is to use the cmd syntax %variable% and then using Environment.ExpandEnvironmentVariables to expand them.

On Azure:

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%'/>

and then to retrieve:

var someSetting = Environment.ExpandEnvironmentVariables(
                     ConfigurationManager.AppSetting("SomeSetting"))

may well work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...