Read file from wwwroot in Blazor web app

I have a JSON file in wwwroot/Resources/form-input-list.json In my razor file I wanna do this:
@code {
private Field[]? fields;

protected override async Task OnInitializedAsync()
{
var json = await Http.GetFromJsonAsync<FormData>("Resources/form-input-list.json");
fields = json?.fields;
}

private class FormData
{
public Metadata metadata { get; set; }
public Field[] fields { get; set; }
}

private class Metadata
{
public int version { get; set; }
public string project { get; set; }
}

private class Field
{
public string type { get; set; }
public string id { get; set; }
public string name { get; set; }
public string labelText { get; set; }
public string helperText { get; set; }
public bool isRequired { get; set; }
public Item[] items { get; set; }
public string placeholderText { get; set; }
}

private class Item
{
public string id { get; set; }
public object value { get; set; }
public string labelText { get; set; }
}
}
@code {
private Field[]? fields;

protected override async Task OnInitializedAsync()
{
var json = await Http.GetFromJsonAsync<FormData>("Resources/form-input-list.json");
fields = json?.fields;
}

private class FormData
{
public Metadata metadata { get; set; }
public Field[] fields { get; set; }
}

private class Metadata
{
public int version { get; set; }
public string project { get; set; }
}

private class Field
{
public string type { get; set; }
public string id { get; set; }
public string name { get; set; }
public string labelText { get; set; }
public string helperText { get; set; }
public bool isRequired { get; set; }
public Item[] items { get; set; }
public string placeholderText { get; set; }
}

private class Item
{
public string id { get; set; }
public object value { get; set; }
public string labelText { get; set; }
}
}
And in my program.cs I have defined it like so:
using nhs_frontend_csharp.frontend.Components;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

builder.Services.AddHttpClient();
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:8081/") });

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();

app.Run();
using nhs_frontend_csharp.frontend.Components;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

builder.Services.AddHttpClient();
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:8081/") });

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();

app.Run();
Yet I keep receiving HTTP Exceptions.
2 Replies
Destination Unknown
with the addScoped I get this error:
CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at System.Net.Http.Json.HttpClientJsonExtensions.<FromJsonAsyncCore>g__Core|12_0[TValue,TJsonOptions](HttpClient client, Task`1 responseTask, Boolean usingResponseHeadersRead, CancellationTokenSource linkedCTS, Func`4 deserializeMethod, TJsonOptions jsonOptions, CancellationToken cancellationToken)
at ....frontend.Components.Pages.Home.OnInitializedAsync() in C:\Projects\nhs-frontend-csharp\nhs-frontend-csharp.frontend\Components\Pages\Home.razor:line 40
at
CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at System.Net.Http.Json.HttpClientJsonExtensions.<FromJsonAsyncCore>g__Core|12_0[TValue,TJsonOptions](HttpClient client, Task`1 responseTask, Boolean usingResponseHeadersRead, CancellationTokenSource linkedCTS, Func`4 deserializeMethod, TJsonOptions jsonOptions, CancellationToken cancellationToken)
at ....frontend.Components.Pages.Home.OnInitializedAsync() in C:\Projects\nhs-frontend-csharp\nhs-frontend-csharp.frontend\Components\Pages\Home.razor:line 40
at
Without the AddScoped I get:
An unhandled exception occurred while processing the request.
InvalidOperationException: An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set.
System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage request)
An unhandled exception occurred while processing the request.
InvalidOperationException: An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set.
System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage request)
Blazor Web server btw.
lycian
lycian6d ago
That doesn't look like the full error message but in the templates they do
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
and
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
Would need more info or the full exception to help more

Did you find this page helpful?