ASPNet Core-macOS开发人员找不到应用程序设置和wwwroot

时间:2018-12-06 14:46:33

标签: .net visual-studio entity-framework asp.net-core entity-framework-core

我正在和一个朋友开始一个新项目,我将使用Windows,他可以在macOS上工作。

该项目使用EntityFrameworkCore,我将连接字符串配置为此(在上下文类中):

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
        optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));

    base.OnConfiguring(optionsBuilder);
}
  

configuration是类型IConfiguration的注入服务。

在Windows中,它可以完美工作,但mac无法说不能使用“ null”作为值。 GetConnectionString实际上对于Mac返回null。

我发现有很多地方说我需要在program.cs中使用UseContentRoot

public static void Main(string[] args)
{
    WebHost.CreateDefaultBuilder(args)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build()
        .Run();
}

但是无论如何它在Mac上不起作用。

此外,在macOS中,Directory.GetCurrentDirectory()将返回<ProjectRoot>/bin/debug/net462,而Windows将返回<ProjectRoot>

因此,我假设它在该路径中找不到appsettings.json,因此失败了。

我还检查了我是否可以访问wwwroot,但不能访问。它始终使用bin而不是项目根目录。

有什么解决方法吗?

谢谢。

PS

与标记为“重复来源”的问题不同:

  

他没有使用UseContentRoot,也没有使用IConfiguration来检索> connectionString。我已经看到了,但不起作用

0 个答案:

没有答案