Misa
Can't read appsettings.json file in c#.net core
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:
And used libs:
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!;
}
}
<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>
7 replies
Can't read appsettings.json file in c#.net core
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();
7 replies