✅ Directory path
Hi, In the appsettings.json file, I've specified a specific path to the data.json file. Unfortunately, the file cannot be found at the provided path:
"Path": "/Users/an/Desktop/Development/Project/Project.Api/App_data/data.json".
The error message indicates: System.IO.DirectoryNotFoundException: Could not find a part of the path '/Library/Frameworks/Mono.framework/Commands:/Applications/Visual Studio.app/Contents/MacOS:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin:/usr/local/bin:~/.dotnet/tools:/usr/local/share/dotnet'.
Occurring on the following code when trying to deserialize:
string fullPath = Path.Combine(parameters.Path);
var json = File.ReadAllText(fullPath);
var searchMethod = JsonConvert.DeserializeObject<List<DefinitionJson>>(json);
I'm using an iOS operating system. Looking forward to a reply 🙂25 Replies
Try using Relative path instead of absolute path
the path it's saying it can't find is not even close to the path you're putting in the config, how are you reading the path in your code?
that looks like an environment
PATH
insteadIt is , that is why I am wondering how pathing works on iOS or dotnet
have you debugged to make sure
parameters.Path
contains the value you expect?parameters.Path "/Library/Frameworks/Mono.framework/Commands:/Applications/Visual Studio.app/Contents/MacOS:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin:/usr/local/bin:~/.dotnet/tools:/usr/local/share/dotnet"
complete different path
so the problem is not in the code you shared
where does that value come from?
originally from appsettings.json
show code
because that's not what's in appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"AppDbConnectionString": "server=localhost;database=db;User=root;Password=admin;"
},
"Path": "/Users/an/Desktop/Development/Project/Project.Api/App_data/data.json"
}
i need to see all the code that is involved in getting from appsettings.json to
parameters.Path
having a value
we know the json has a certain value and parameters.Path
has the wrong value
so the problem is somwhere in the middleSure let me organize the code
Repo
public class DefinitionRepository : IDefinitionRepository
{
private readonly IParameters parameters;
public DefinitionRepository(IParameters parameters)
{
this.parameters = parameters;
}
public async Task<IEnumerable<DefinitionJson>> Get()
{
string fullPath = Path.Combine(parameters.Path);
var json = File.ReadAllText(fullPath);
var searchMethod = JsonConvert.DeserializeObject<List<DefinitionJson>>(json);
return searchMethod;
}
}
this still doesn't tell me where
parameters.Path
gets a valueInterface `namespace Definition.Infrastructure
{
public interface IParameters
{
string Path { get; }
}
}z
that's all i want to know
Parameter dir
namespace Definition.Api.Parameters
{
public class Parameters : IParameters
{
private readonly IConfiguration configuration;
public Parameters(IConfiguration configuration)
{
this.configuration = configuration;
}
public string Path => configuration["Path"] ?? "";
}
}
as an aside, this isn't how you should be doing this
you should bind your configuration class during application startup instead of injecting IConfiguration
anyway, i'd put a breakpoint on that getter and check the configuration in general to see if it has reasonable values
injected in startup :
builder.Services.AddScoped<IParameters, Parameters>();
ty thoughthat's not what i'm saying
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-8.0#the-options-pattern
if the configuration key
Path
has the wrong value itself, i would try just changing it to a different name in case it's conflicting with the environment variable PATH
Hahaha
that actually did the job
yeah, environment variables take precedence over basically every other configuration source
Thank you appreciate it alot. Have been stuck for somewhat 4 hours now
for future reference this is described in the configuration docs https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0#default-application-configuration-sources
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
appsettings.json:
src/Foo/FooOptions.cs:
src/Foo/FooServiceCollectionExtensions.cs:
Program.cs / Startup.cs:
Bar.cs:
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View