Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调试中ASP.NET核心启动时未找到config.json_C#_Json_Asp.net Web Api_Asp.net Core - Fatal编程技术网

C# 调试中ASP.NET核心启动时未找到config.json

C# 调试中ASP.NET核心启动时未找到config.json,c#,json,asp.net-web-api,asp.net-core,C#,Json,Asp.net Web Api,Asp.net Core,我通过Yeoman创建了一个基本的WebAPI项目(注意:我正在处理的一个“真实”项目也有同样的问题,但是yo也演示了这个问题),目标是OSX上的netcoreapp1.0 在命令行上,它通过dotnet restore,dotnet build,dotnet run恢复、构建并运行良好 但是,当我使用Visual Studio代码并使用Debug时,我总是会收到错误:“未找到配置文件'config.json',并且不是可选的。”它指向我的Main方法的第一行 这是我的程序.cs入口点: pub

我通过Yeoman创建了一个基本的WebAPI项目(注意:我正在处理的一个“真实”项目也有同样的问题,但是
yo
也演示了这个问题),目标是OSX上的
netcoreapp1.0

在命令行上,它通过
dotnet restore
dotnet build
dotnet run
恢复、构建并运行良好

但是,当我使用Visual Studio代码并使用Debug时,我总是会收到错误:“未找到配置文件'config.json',并且不是可选的。”它指向我的Main方法的第一行

这是我的
程序.cs
入口点:

public static void Main(string[] args)
{            
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}
添加
Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace WebAPIApplication
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("config.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
        }
    }
}
如果需要任何其他信息,请让我知道,我很乐意补充

正如Gerardo所建议的那样,打印出
Directory.GetCurrentDirectory()
会得到两种不同的输出

从命令行(有效):
~/Projects/Path/WebApiApplication/
从VSCode中的调试:
~/Projects/Path


我还注意到,输出将进入
~/Projects/Path/WebApiApplication/bin/Debug/netcoreapp1.0/
,但没有任何文件(包括
config.json
)。

看起来
dotnet run
是站在项目文件夹上运行的,工作正常,但
VS-code
调试模式不正常

这是在配置文件
.vscode\launch.json
中的
VS code
中配置的。 找一行写着:

       "cwd": "${workspaceRoot}",
并将其更改为:

        "cwd": "${workspaceRoot}/MyProjectFolder",
您需要更改启动文件夹(
cwd
),使其与
project.json
/
config.json
/等的位置相匹配


此处的详细信息:

移动YourSolution/src/YourProject“目录中的.vscode文件夹,错误地在YourSolution目录中的vscode中打开了项目,并按照建议的答案调整配置,运行了应用程序,但存在配置问题


因此,在VSCode中,您必须从项目内部打开项目,默认配置将正常运行。

您的启动类是否包含类似于
new ConfigurationBuilder().AddJsonFile(“config.json”)
?@GerardoGrignoli的内容,是的。我将把Startup.cs添加到我的问题中。
ConfigurationBuilder
正在使用相对于
.SetBasePath(env.ContentRootPath
,它来自
Main
.UseContentRoot(Directory.GetCurrentDirectory())的路径
.So:可能是VS代码调试在不同的目录下启动了应用程序吗?请添加此
控制台.WriteLine(directory.GetCurrentDirectory())
作为
Main
的第一行,比较“dotnet run”和VS code Debug之间的输出。@GerardoGrignoli谢谢。肯定提供了一些有趣的信息。在我的问题中加入细节。当你提到
.vscode\launch.json时,我意识到-我打开的文件夹比我打开的许多项目都要高一步(有些是别人的图书馆)。这意味着有一个顶级的
.vscode\launch.json
。如果我只打开我想要使用的项目,调试效果会很好。我想这会引出另一个问题-我如何正确地在vscode中引入属于项目一部分的其他库?无论如何,将您的答案标记为良好,因为它确实让我走上了正确的轨道。谢谢s!您是否尝试过按照我在回答中的建议编辑
launch.json
文件?
        "cwd": "${workspaceRoot}/MyProjectFolder",