如何在 azure 中设置 Web 应用程序的连接字符串?

问题描述 投票:0回答:3

我使用 ASP.Net 5 Web 应用程序模板创建了一个 Web 应用程序,并将其发布到 Azure Web 应用程序。

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

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

c# asp.net azure
3个回答
3
投票

请尝试这个...

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

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

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

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

Azure Web 应用程序配置

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

启动.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 的连接字符串,一切都会正常工作。


1
投票

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


0
投票

现在可以从应用服务在 Azure 中找到连接字符串 --> 选择网站 --> 如果需要,选择 webslot --> 设置部分/环境变量

© www.soinside.com 2019 - 2024. All rights reserved.