Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access connection strings in ASP.NET 5 on Azure

In ASP.NET 5, how do we programmatically access an Azure Web App's connection strings? I've been able to retrieve the TEST_APP_SETTINGS value but not the TestConnString one.

App Settings and Connection Strings

Here is what I have tried:

services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

I'm afraid that AppSettings doesn't exist. I've also done this and the app settings show up but the connection strings do not.

Startup.cs

// Setup configuration sources.
Configuration = new Configuration()
    .AddJsonFile("config.json")
    .AddEnvironmentVariables();

// Allow access from *.cshtml
services.AddSingleton<Configuration>(provider => 
{ 
    return Configuration as Configuration; 
});

Dev.cshtml

@inject Microsoft.Framework.ConfigurationModel.Configuration config;

<dl>
    @foreach(var k in @config.GetSubKeys())
    {
        <dt>@k.Key</dt>
        <dd>@config.Get(k.Key)</dd>
    }
</dl>
like image 395
Shaun Luttin Avatar asked Dec 20 '25 14:12

Shaun Luttin


1 Answers

The connection strings are set as environment variables. Therefore, first you have to add the environment variable configuration source and then the connection strings will be named Data:NAME:ConnectionString where NAME is the name of the connection string in the portal.

For example, if your connection string is "ServerConnection" then you access it using using Data:ServerConnection:ConnectionString

Here are the tests that validate the mapping environment configuration mappings, they might explain it better.

like image 131
Victor Hurdugaci Avatar answered Dec 22 '25 05:12

Victor Hurdugaci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!