SweetTomato
SweetTomato

Reputation: 569

How to access App Settings from Azure in Asp.net MVC

I have added an app setting in Azure for my Web Application that I named "SecretKey". When I try to access it in my asp.net mvc app, the key returns null.

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

I've tried to look up information and I can't find if there is anything I need to do beyond adding the key in Azure. Why is it not able to access it? Is there something I need to do elsewhere in my application to get it to access it?

Upvotes: 0

Views: 285

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222700

You can access it as follows,

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

public IConfiguration Configuration { get; }

and use it as

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

Upvotes: 1

Sergii Bidnyi
Sergii Bidnyi

Reputation: 306

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

in your example your are reading env vars but not app settings

Upvotes: 0

Related Questions