如何在本地获取Azure应用程序设置?

时间:2017-12-10 22:31:04

标签: c# asp.net azure

我尝试将SendGrid与我的应用程序集成,该应用程序托管在Azure上。

其中一条说明是将API密钥存储为Azure中的应用程序设置,我已经完成了。

我尝试在本地测试,当我尝试获取该设置时,它会以null的形式返回:

var apiKey = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");

我想也许它需要在我的web.config中,所以我在那里添加它,它仍然以null的形式返回。

有没有办法在本地获取这些设置?或者我是否需要以某种方式确定我在本地运行,并使用其他方法而不是GetEnvironmentVariable()

1 个答案:

答案 0 :(得分:3)

是的,您希望将SendGrid API密钥放在web.config文件的部分中。它看起来像是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    ... other web.config settings ....
    <appSettings>
        <add key="SendGridAPIKey" value="[Your key goes here]" />
    </appSettings>
</configuration>

您可以使用以下代码行检索此值:

string apiKey = System.Configuration.ConfigurationManager.AppSettings["SendGridAPIKey"];

这将在本地和Azure Web App中工作。

相关问题