我在运行 dotnet watch run 时遇到以下错误。
Unhandled Exception: System.FormatException: Could not parse the JSON file.
Error on line number '0': ''. -
--> Newtonsoft.Json.JsonReaderException: Error reading JObject from
JsonReader. Path '', line 0, position 0
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings
settings) at
Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.Parse(Stream input)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream)
--- End of inner exception stack trace ---
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at WebApplicationBasic.Startup..ctor(IHostingEnvironment env) in \Microservices\TestProject\Startup.cs:line 25
我确认没有任何错误的 json 文件。如果有帮助,请添加我的项目文件。这可能无关紧要,但我不确定在哪里看。
appsettings.json:
{
"ASPNETCORE_ENVIRONMENT": "Development",
"ConnectionStrings": {
"Default": "Server=localhost; database=TestProject; Integrated Security=True"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
启动.cs:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<TestDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddMvc();
// services.AddMvcCore()
// .AddApiExplorer();
return services.BuildServiceProvider();
}
任何建议从哪里开始寻找?
我怀疑文件是空的,或者它以 UTF-8 BOM 开头。
线索在这里:
Error on line number '0': ''. -
它抱怨的东西是看不见的: ''
所以可能在预期的地方没有发现任何东西,或者它是某种不可见的特征,其中包括'\uFEFF'
BOM。
出于精神错乱的原因,JSON 文件在开始时被明确禁止使用 BOM,即使该代码点可以出现在文件中的任何其他位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句