R
Railway5mo ago
Misa

Can't read appsettings.json file in c#.net core

Hello friends. I am facing a problem in my project. The problem When deploying the application, the "appsettings.json" file is not found/read when the application is run. The file is copied to the deploy page. I don't know if it was the way I programmed it that is generating this error. Project data ID: 608729b3-caba-4f38-82e1-b0830df0e764 Language: c#.net core Type: RESTFull API.
No description
3 Replies
Percy
Percy5mo ago
Project ID: N/A
Brody
Brody5mo ago
is that file in your repo?
Misa
Misa5mo ago
Yes the file is in the repository Follow my c#.net code, Program.cs:
using ValveSteamDataTools.Domain.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddAutoMapper(typeof(AutoMapperProfile));

builder.Services.AddScoped<IValveSteamService, ValveSteamService>();
builder.Services.AddScoped<IPlayerService, PlayerService>();
builder.Services.AddScoped<IGameService, GameService>();

var policyName = "ValeSteamDataTools";

builder.Services.AddCors(options =>
{
options.AddPolicy
(
name: policyName,
policy => policy
.WithOrigins(AppSettings.Frontend.Url)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
);
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseCors(policyName);
app.UseAuthorization();
app.MapControllers();
app.Run();
using ValveSteamDataTools.Domain.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddAutoMapper(typeof(AutoMapperProfile));

builder.Services.AddScoped<IValveSteamService, ValveSteamService>();
builder.Services.AddScoped<IPlayerService, PlayerService>();
builder.Services.AddScoped<IGameService, GameService>();

var policyName = "ValeSteamDataTools";

builder.Services.AddCors(options =>
{
options.AddPolicy
(
name: policyName,
policy => policy
.WithOrigins(AppSettings.Frontend.Url)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
);
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseCors(policyName);
app.UseAuthorization();
app.MapControllers();
app.Run();
I did the reading a little differently in my program. Handling reading in an AppSettings.cs class, using static properties and libraries for reading the IConfiguration file This is the result of the class:
namespace ValveSteamDataTools.Domain.Models
{
public class AppSettings
{
public static class ValveSteam
{
public static class BaseUrl
{
public static string Api { get => GetValeuFromKey("ValveSteam:BaseUrl:Api"); }
public static string Store { get => GetValeuFromKey("ValveSteam:BaseUrl:Store"); }
}
}

public static class Frontend
{
public static string Url { get => GetValeuFromKey("Frontend:Url"); }
}

private static IConfigurationRoot GetAppSettings()
{
IConfigurationBuilder builder = new ConfigurationBuilder();
var appSettingFile = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");

if (!File.Exists(appSettingFile))
throw new Exception("The appsettings.json file not found in project.");

builder.AddJsonFile(appSettingFile);
IConfigurationRoot root = builder.Build();

return root;
}

private static string GetValeuFromKey(string key) => GetAppSettings().GetSection(key).Value!;
}
}
namespace ValveSteamDataTools.Domain.Models
{
public class AppSettings
{
public static class ValveSteam
{
public static class BaseUrl
{
public static string Api { get => GetValeuFromKey("ValveSteam:BaseUrl:Api"); }
public static string Store { get => GetValeuFromKey("ValveSteam:BaseUrl:Store"); }
}
}

public static class Frontend
{
public static string Url { get => GetValeuFromKey("Frontend:Url"); }
}

private static IConfigurationRoot GetAppSettings()
{
IConfigurationBuilder builder = new ConfigurationBuilder();
var appSettingFile = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");

if (!File.Exists(appSettingFile))
throw new Exception("The appsettings.json file not found in project.");

builder.AddJsonFile(appSettingFile);
IConfigurationRoot root = builder.Build();

return root;
}

private static string GetValeuFromKey(string key) => GetAppSettings().GetSection(key).Value!;
}
}
And used libs:
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
</ItemGroup>

</Project>