访问 Azure 上 ASP.NET 5 中的连接字符串

2024-04-18

在 ASP.NET 5 中,我们如何以编程方式访问 Azure Web 应用程序的连接字符串?我已经能够检索到TEST_APP_SETTINGS值但不是TestConnString one.

这是我尝试过的:

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

恐怕AppSettings不存在。我也这样做了,应用程序设置显示了,但连接字符串没有显示。

启动.cs

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

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

开发.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>

连接字符串设置为环境变量。因此,首先你必须添加环境变量配置源 https://github.com/aspnet/MusicStore/blob/master/src/MusicStore/Startup.cs#L24然后连接字符串将被命名Data:NAME:ConnectionString where NAME是门户中连接字符串的名称。

例如,如果您的连接字符串是“ServerConnection”,那么您可以使用以下方式访问它:Data:ServerConnection:ConnectionString

Here https://github.com/aspnet/Configuration/blob/d53fd365ff3c983ca57700c8f26213e6c8dd968b/test/Microsoft.Framework.ConfigurationModel.Test/EnvironmentVariablesConfigurationSourceTest.cs#L53-L76是验证映射环境配置映射的测试,它们可能会更好地解释它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

访问 Azure 上 ASP.NET 5 中的连接字符串 的相关文章

随机推荐