Inject framework HttpClient to support prerendering behavior on server instead of baking in the HttpClient on the client project
This commit is contained in:
@@ -3,12 +3,18 @@ using DeepDrftModels.Models;
|
||||
using NetBlocks.Models;
|
||||
using System.Text.Json;
|
||||
using System.Web;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace DeepDrftWeb.Client.Clients;
|
||||
|
||||
public class TrackClient : ApiClient<ClientConfig>
|
||||
public class TrackClient
|
||||
{
|
||||
public TrackClient(ClientConfig config) : base(config) { }
|
||||
private readonly HttpClient _http;
|
||||
|
||||
public TrackClient(HttpClient http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public async Task<ApiResult<PagedResult<TrackEntity>>> GetPage(
|
||||
int pageNumber,
|
||||
@@ -16,24 +22,20 @@ public class TrackClient : ApiClient<ClientConfig>
|
||||
string? sortColumn = null,
|
||||
bool sortDescending = false)
|
||||
{
|
||||
var uriBuilder = new UriBuilder(http.BaseAddress!)
|
||||
{
|
||||
Path = "api/track/page"
|
||||
var queryArgs = new Dictionary<string, string?>(){
|
||||
["pageNumber"] = pageNumber.ToString(),
|
||||
["pageSize"] = pageSize.ToString()
|
||||
};
|
||||
|
||||
var query = HttpUtility.ParseQueryString(string.Empty);
|
||||
query["pageNumber"] = pageNumber.ToString();
|
||||
query["pageSize"] = pageSize.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(sortColumn))
|
||||
query["sortColumn"] = sortColumn;
|
||||
queryArgs["sortColumn"] = sortColumn;
|
||||
|
||||
if (sortDescending)
|
||||
query["sortDescending"] = "true";
|
||||
queryArgs["sortDescending"] = "true";
|
||||
|
||||
uriBuilder.Query = query.ToString();
|
||||
|
||||
var response = await http.GetAsync(uriBuilder.Uri);
|
||||
string query = QueryString.Create(queryArgs).ToString();
|
||||
|
||||
var response = await _http.GetAsync($"api/track/page{query}");
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
var dto = JsonSerializer.Deserialize<ApiResultDto<PagedResult<TrackEntity>>>(json, new JsonSerializerOptions
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.*" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.0" />
|
||||
<PackageReference Include="MudBlazor" Version="8.*" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -4,8 +4,11 @@ using MudBlazor.Services;
|
||||
|
||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
|
||||
Console.WriteLine(builder.HostEnvironment.BaseAddress);
|
||||
builder.Services.AddScoped<HttpClient>(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
|
||||
|
||||
builder.Services.AddMudServices();
|
||||
|
||||
Startup.ConfigureDomainServices(builder.Services, builder.HostEnvironment.BaseAddress);
|
||||
Startup.ConfigureDomainServices(builder.Services);
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
|
||||
@@ -7,13 +7,9 @@ namespace DeepDrftWeb.Client;
|
||||
|
||||
public static class Startup
|
||||
{
|
||||
public static void ConfigureDomainServices(IServiceCollection services, string baseAddress)
|
||||
public static void ConfigureDomainServices(IServiceCollection services)
|
||||
{
|
||||
// HTTP Client for Server API
|
||||
services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(baseAddress) });
|
||||
|
||||
// Track Client
|
||||
services.AddSingleton(new ClientConfig(baseAddress));
|
||||
services.AddScoped<TrackClient>();
|
||||
services.AddScoped<TrackGalleryViewModel>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user