配置.在ASP上找不到json.. NET Core启动调试
本文关键字:NET Core 启动 调试 json 找不到 ASP 配置 | 更新日期: 2023-09-27 18:02:52
我通过Yeoman创建了一个基本的WebAPI项目(注意:我在一个"真正的"项目中也有同样的问题,但yo
也证明了这个问题),目标是OSX上的netcoreapp1.0
。
在命令行上,它通过dotnet restore
、dotnet build
、dotnet run
恢复、构建并运行良好。
当我在Visual Studio Code中使用调试时,然而,我总是收到错误:"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
调试模式不是。
这是在VS Code
中的配置文件.vscode'launch.json
中配置的。找到下面这行:
"cwd": "${workspaceRoot}",
并将其改为:
"cwd": "${workspaceRoot}/MyProjectFolder",
你需要改变启动文件夹(cwd
),使其与你的project.json
/config.json
/等的位置相匹配。
更多信息在这里:Visual Studio代码启动配置
将. VSCode文件夹移到"YourSolution/src/YourProject"目录下,错误地打开了YourSolution目录下VSCode中的项目,并通过调整配置来运行应用程序,但存在配置问题。
所以在VSCode中你必须从YourProject中打开项目,默认配置将正常运行