我已经通过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
)。
答案 0 :(得分:2)
似乎dotnet run
正在项目文件夹上运行并且工作正常,但VS Code
调试模式不是。
这是在VS Code
配置文件.vscode\launch.json
中配置的。
找到说:
"cwd": "${workspaceRoot}",
并将其更改为:
"cwd": "${workspaceRoot}/MyProjectFolder",
您需要更改启动文件夹(cwd
),使其与project.json
/ config.json
/等的位置相匹配。
答案 1 :(得分:0)
移动“YourSolution / src / YourProject”目录中的.vscode文件夹,错误地在YourSolution目录中的VSCode中打开项目,并通过调整配置,如建议的答案运行应用程序,但配置问题。
因此,在VSCode中,您必须从YourProject内部打开项目,默认配置才能正常运行。