如何在Asp.net MVC中从Azure访问App设置?

问题描述 投票:0回答:2

我在 Azure 中为我的 Web 应用程序添加了一个应用程序设置,我将其命名为 "SecretKey"。当我尝试在我的 asp.net mvc 应用程序中访问它时,密钥返回空。

var key = Environment.GetEnvironmentVariables("SecretKey");

我试图查找资料,但除了在Azure中添加密钥外,我找不到是否有什么需要做的。为什么无法访问它?我是否需要在应用程序的其他地方做一些事情才能让它访问它?

asp.net-mvc azure
2个回答
1
投票

你可以按以下方式访问它。

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);
  }

0
投票

核对 https:/docs.microsoft.comen-usaspnetcorefundamentalsconfiguration?view=aspnetcore-3.1。

在你的例子中,你读取的是环境变量,而不是应用程序设置。

© www.soinside.com 2019 - 2024. All rights reserved.