gpt4 book ai didi

c# - 在调试中的 ASP.NET Core 启动时找不到 config.json

转载 作者:行者123 更新时间:2023-12-04 12:18:49 25 4
gpt4 key购买 nike

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

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

但是,当我在 Visual Studio Code 中使用 Debug 时,我总是收到错误:"The configuration file 'config.json' was not found and is not optional."它指向我 Main 的第一行方法。

这是我的 Program.cs入口点:

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

host.Run();
}

这是 project.json :
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
},

"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},

"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},

"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},

"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},

"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"appsettings.json",
"web.config"
]
},

"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
},

"tooling": {
"defaultNamespace": "WebAPIApplication"
}
}

添加 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 Debug模式不是。

这是在 VS Code 中配置的在配置文件中 .vscode\launch.json .
找到说:

       "cwd": "${workspaceRoot}",

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

您需要更改启动文件夹( cwd ),使其与您的 project.json 的位置相匹配/ config.json/等等。

更多信息在这里: Visual Studio Code Launch Configurations

关于c# - 在调试中的 ASP.NET Core 启动时找不到 config.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38986736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com