如何在Asp.net MVC中从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);
}

检查https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1

在您的示例中,您正在读取env-var,但没有读取应用程序设置

最新更新