Asp.net mvc 如何在Asp.net MVC中从Azure访问应用程序设置

Asp.net mvc 如何在Asp.net MVC中从Azure访问应用程序设置,asp.net-mvc,azure,Asp.net Mvc,Azure,我在Azure中为我的Web应用程序添加了一个名为“SecretKey”的应用程序设置。当我尝试在asp.net mvc应用程序中访问它时,该键返回null var key = Environment.GetEnvironmentVariables("SecretKey"); 我试图查找信息,但除了在Azure中添加密钥外,我找不到是否还有其他需要做的事情。为什么它不能访问它?我需要在我的应用程序的其他地方做些什么才能让它访问它吗?检查 在您的示例中,您正在读取环境变量而不是应用程序设置,您可

我在Azure中为我的Web应用程序添加了一个名为“SecretKey”的应用程序设置。当我尝试在asp.net mvc应用程序中访问它时,该键返回null

var key = Environment.GetEnvironmentVariables("SecretKey");
我试图查找信息,但除了在Azure中添加密钥外,我找不到是否还有其他需要做的事情。为什么它不能访问它?我需要在我的应用程序的其他地方做些什么才能让它访问它吗?

检查


在您的示例中,您正在读取环境变量而不是应用程序设置,您可以按如下方式访问它:

public Startup(IConfiguration configuration)
{
  Configuration = configuration;
}

public IConfiguration Configuration { get; }
并将其用作

 public void ConfigureServices(IServiceCollection services)
  {
     var connection = Configuration.GetConnectionString("SecretKey");
     services.AddDbContext<ShelterPZ_DBContext>(options => options.UseSqlServer(connection));
     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
  }
public void配置服务(IServiceCollection服务)
{
var connection=Configuration.GetConnectionString(“SecretKey”);
services.AddDbContext(options=>options.UseSqlServer(connection));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}

对于此部分“.GetConnectionString(“SecretKey”)”,这不是只有当我将其添加到Azure的连接字符串部分而不是应用程序设置部分时才会发生的吗/环境变量和应用程序设置之间有什么区别?我以为代码可以从azure访问应用程序设置?