简体   繁体   English

ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”); 找不到文件

[英]ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”); not finding file

So I've finally got round to looking at Core and I've fallen at the first hurdle.所以我终于有时间看看 Core 并且我已经在第一个障碍处摔倒了。 I'm following the Pluralsight ASP.NET Core Fundamentals course and I'm getting an exception when trying too add the appsettings.json file to the configuration builder.我正在学习 Pluralsight ASP.NET Core Fundamentals 课程,但在尝试将 appsettings.json 文件添加到配置生成器时出现异常。

public Startup()
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json");

    Configuration = builder.Build();
}

The error I'm getting is The configuration file 'appsettings.json' was not found and is not optional.我得到的错误是找不到配置文件 'appsettings.json' 并且不是可选的。 But I have created the directly under my solution just like in the course video.但是我已经在我的解决方案下直接创建了,就像在课程视频中一样。

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json");

    Configuration = builder.Build();
}

This seems to do the trick.这似乎可以解决问题。 However unsure this is the proper way to do it.但是不确定这是正确的方法。 Kinda feels like a hack.有点像黑客。

您需要添加以下包:

    "Microsoft.Extensions.Configuration.Json": "1.0.0"

Another way:另一种方式:

appsettings.json : appsettings.json

{
    "greeting": "A configurable hello, to you!"
}

Startup.cs : Startup.cs

using Microsoft.Extensions.Configuration; // for using IConfiguration
using System.IO; // for using Directory

public class Startup
{
    public IConfiguration Configuration { get; set; }

    public Startup()
    {
        var builder = new ConfigurationBuilder();
        builder.SetBasePath(Directory.GetCurrentDirectory());
        builder.AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }    
}

In the Configure method:Configure方法中:

app.Run(async (context) =>
{
    // Don't use:
    // string greeting = Configuration["greeting"]; // null

    string greeting = Configuration.GetSection("greeting").Value;
    await context.Response.WriteAsync(greeting)
});

An alternative solution I found from this blog post works as well.我从这篇博文中找到的另一种解决方案也有效。 It has the added benefit of not needing to modify the Startup.cs file's Startup method signature.它的另一个好处是不需要修改 Startup.cs 文件的 Startup 方法签名。

In the buildOptions section add copyToOutput with the name of the file.在 buildOptions 部分中添加带有文件名称的 copyToOutput。

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true,
    "copyToOutput": "appsettings.json"
  },
  .... The rest of the file goes here ....

Actually for this you need to provide root path from your environment variable so you need to pass IHostingEnvironment reference to provide root path:实际上,为此您需要从环境变量中提供根路径,因此您需要传递IHostingEnvironment引用以提供根路径:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
                          .SetBasePath(env.ContentRootPath)
                          .AddJsonFile("appsettings.json");
    Configuration = builder.Build();
}

and if you can't find AddJsonFile method then you have to add using Microsoft.Extensions.Configuration.Json;如果找不到 AddJsonFile 方法,则必须using Microsoft.Extensions.Configuration.Json;添加using Microsoft.Extensions.Configuration.Json;

右键单击 appsettings.json -> 属性,然后确保将复制到输出目录设置为“始终复制”

In .NET Core 2.0, you would update the .csproj file to copy the JSON file to the output directory so that it can be accessed, like so:在 .NET Core 2.0 中,您将更新 .csproj 文件以将 JSON 文件复制到输出目录,以便可以访问它,如下所示:

<ItemGroup>
    <Folder Include="wwwroot\" />
    <None Include="appsettings.json" CopyToOutputDirectory="Always" />
</ItemGroup>

The answers that suggest adding .SetBasePath(env.ContentRootPath) in Startup.cs depend upon a prior step.建议在Startup.cs添加.SetBasePath(env.ContentRootPath)的答案取决于先前的步骤。

First , add the line .UseContentRoot(Directory.GetCurrentDirectory()) to your WebHostBuilder construction in Program.cs like so:首先,将.UseContentRoot(Directory.GetCurrentDirectory())行添加到Program.cs的 WebHostBuilder 构造中,如下所示:

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddEnvironmentVariables()
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

Then the following will work:然后以下将起作用:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json");

    Configuration = builder.Build();
}

If you have done anything with Project Debug Properties, then you may have inadvertently overwritten the starting directory:如果您对 Project Debug Properties 进行了任何操作,那么您可能无意中覆盖了起始目录:

Project -> Right-click -> Properties -> Debug -> Profile and then look at the entry in Working Directory . Project -> Right-click -> Properties -> Debug -> Profile然后查看Working Directory中的条目。

The simplest is that it be blank.最简单的是它是空白的。

你需要从Microsoft.Extensions中获取Configuration.Json包,或者只是将Microsoft.Extensions.Configuration.Json添加到y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM