2016-05-13 63 views
3

我使用ASP.Net 5 Web应用程序模板创建了一个Web应用程序,并将其发布到Azure Web应用程序。如何在azure中为我的web应用程序设置连接字符串?

在我的网络应用程序的设置部分,我能够创建一个数据连接。 如何告诉我的Web应用程序使用此数据连接。

[更新] 我appsettings.json连接字符串的名称是DefaultConnection

+1

你看看这个https://azure.microsoft.com/en-us/documentation/articles/web-sites-configure/? – Mostafiz

+0

我想这可能会帮助你https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/ – Mostafiz

+0

微软Azure Configuration Manager NuGet包为您提供统一的API。也许这适合你的解决方案。 –

回答

3

请试试这个...

  1. 转到Azure的Web应用程序>配置>连接字符串。

  2. 添加名为DefaultConnection的连接字符串。

  3. 使用Configuration.Get(“Data:DefaultConnection:ConnectionString”)来访问它。

实施例使用timesheet_db代替DefaultConnection 这是我自己的时间表应用程序的例子。我的连接字符串被命名为timesheet_db。只需用DefaultConnection替换该字符串的所有实例即可将该示例适用于您的用例。

Azure的Web应用程序配置 enter image description here

Azure的web应用程序的服务控制管理器 在https://myWebAppName.scm.azurewebsites.net/Env在线服务控制管理器将显示连接字符串。

enter image description here

Startup.cs 在启动安装程序的配置设置,这样的环境变量覆盖config.json

public IConfiguration Configuration { get; set; } 
public Startup() 
{ 
Configuration = new Configuration() 
    .AddJsonFile("config.json") 
    .AddEnvironmentVariables(); <----- will cascade over config.json 
} 

配置在启动数据库。

public void ConfigureServices(IServiceCollection services) 
{ 
    services 
    .AddEntityFramework() 
    .AddSqlServer() 
    .AddDbContext<ProjectContext>(options => 
    { 
     var connString = 
      Configuration.Get("Data:timesheet_db:ConnectionString"); 
     options.UseSqlServer(connString); 
    }); 
    } 

当然,该示例使用名为timesheet_db的连接字符串。对于你来说,用你自己的名为DefaultConnection的连接字符串替换它的所有实例,一切都会工作。

+0

我找不到一个菜单选项,称为在我的网络应用程序内配置 –

+0

我正在使用应用程序服务,然后点击我设置的网络应用程序。有一个设置菜单,但没有配置菜单选项。 –

1

应用程序服务,然后选择网络应用程序,然后选择设置,然后选择应用程序设置。 向下滚动到连接字符串。 确保连接被命名为DefaultConnection

相关问题