在Azure上的ASP.NET 5中访问连接字符串 [英] Access connection strings in ASP.NET 5 on Azure

查看:92
本文介绍了在Azure上的ASP.NET 5中访问连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我尝试过的:

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

恐怕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

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

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>

推荐答案

将连接字符串设置为环境变量.因此,首先,您必须添加环境变量配置源,然后将连接字符串命名为Data:NAME:ConnectionString,其中NAME是门户中连接字符串的名称.

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.

例如,如果您的连接字符串是"ServerConnection",则可以使用Data:ServerConnection:ConnectionString

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.

这篇关于在Azure上的ASP.NET 5中访问连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆