C
C#9mo ago
Nohxi

✅ 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 🙂
No description
25 Replies
The Fog from Human Resources
Try using Relative path instead of absolute path
Jimmacle
Jimmacle9mo ago
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 instead
Nohxi
NohxiOP9mo ago
It is , that is why I am wondering how pathing works on iOS or dotnet
Jimmacle
Jimmacle9mo ago
have you debugged to make sure parameters.Path contains the value you expect?
Nohxi
NohxiOP9mo ago
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
Jimmacle
Jimmacle9mo ago
so the problem is not in the code you shared where does that value come from?
Nohxi
NohxiOP9mo ago
originally from appsettings.json
Jimmacle
Jimmacle9mo ago
show code because that's not what's in appsettings.json
Nohxi
NohxiOP9mo ago
{ "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" }
Jimmacle
Jimmacle9mo ago
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 middle
Nohxi
NohxiOP9mo ago
Sure 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; } }
Jimmacle
Jimmacle9mo ago
this still doesn't tell me where parameters.Path gets a value
Nohxi
NohxiOP9mo ago
Interface `namespace Definition.Infrastructure { public interface IParameters { string Path { get; } } }z
Jimmacle
Jimmacle9mo ago
that's all i want to know
Nohxi
NohxiOP9mo ago
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"] ?? ""; } }
Jimmacle
Jimmacle9mo ago
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
Nohxi
NohxiOP9mo ago
injected in startup : builder.Services.AddScoped<IParameters, Parameters>(); ty though
Jimmacle
Jimmacle9mo ago
that'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
Nohxi
NohxiOP9mo ago
Hahaha that actually did the job
Jimmacle
Jimmacle9mo ago
yeah, environment variables take precedence over basically every other configuration source
Nohxi
NohxiOP9mo ago
Thank you appreciate it alot. Have been stuck for somewhat 4 hours now
Jimmacle
Jimmacle9mo ago
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX9mo ago
appsettings.json:
{
"My": {
"Foo": {
"Kix": 5
}
}
}
{
"My": {
"Foo": {
"Kix": 5
}
}
}
src/Foo/FooOptions.cs:
public class FooOptions
{
public const string SectionName = "My:Foo";

public string Bar {get;set;} = "default value for bar";
public int Kix {get;set;} = -1;
public DateTime? Pouet {get;set;} = default;
}
public class FooOptions
{
public const string SectionName = "My:Foo";

public string Bar {get;set;} = "default value for bar";
public int Kix {get;set;} = -1;
public DateTime? Pouet {get;set;} = default;
}
src/Foo/FooServiceCollectionExtensions.cs:
namespace Microsoft.Extensions.DependencyInjection; // <==== recommanded for service.Add so that you don't clutter Startup file

public class FooServiceCollectionExtensions
{
public static IServiceCollection AddFoo(this IServiceCollection services) =>
services
.AddOptions<FooOptions>()
.BindConfiguration(FooOptions.SectionName)
.Validate(options => options.Kix >= 0, $"The configuration key '{FooOptions.SectionName}:{nameof(Kix)}' cannot be negative")
;

public static IServiceCollection AddFoo(this IServiceCollection services, Action<FooOptions> configure) =>
services
.AddFoo()
.Configure(configure);
namespace Microsoft.Extensions.DependencyInjection; // <==== recommanded for service.Add so that you don't clutter Startup file

public class FooServiceCollectionExtensions
{
public static IServiceCollection AddFoo(this IServiceCollection services) =>
services
.AddOptions<FooOptions>()
.BindConfiguration(FooOptions.SectionName)
.Validate(options => options.Kix >= 0, $"The configuration key '{FooOptions.SectionName}:{nameof(Kix)}' cannot be negative")
;

public static IServiceCollection AddFoo(this IServiceCollection services, Action<FooOptions> configure) =>
services
.AddFoo()
.Configure(configure);
Program.cs / Startup.cs:
services.AddFoo();
// or
services.AddFoo(fooOptions => fooOptions.Kix = 12);
services.AddFoo();
// or
services.AddFoo(fooOptions => fooOptions.Kix = 12);
Bar.cs:
public class Bar
{
private readonly FooOptions _fooOptions;

// .Value in ctor is fine only if it's always ever a non-changing value (no reload and/or no scoped resolution)
public Bar(IOptions<FooOptions> fooOptions)
=> _fooOptions = fooOptions.Value;
}
public class Bar
{
private readonly FooOptions _fooOptions;

// .Value in ctor is fine only if it's always ever a non-changing value (no reload and/or no scoped resolution)
public Bar(IOptions<FooOptions> fooOptions)
=> _fooOptions = fooOptions.Value;
}
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server