Refactor API base URL config to use AppSettings class
Replaced direct IConfiguration usage with a strongly-typed AppSettings class for accessing the API base URL. Registered AppSettings with DI and updated Dashboard.razor to use IOptions<AppSettings>. Updated using statements and DI setup for improved type safety and centralized configuration management.
This commit is contained in:
6
DbFirst.BlazorWebApp/AppSettings.cs
Normal file
6
DbFirst.BlazorWebApp/AppSettings.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace DbFirst.BlazorWebApp;
|
||||
|
||||
public class AppSettings
|
||||
{
|
||||
public string ApiBaseUrl { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
@page "/dashboard"
|
||||
@page "/dashboards/{DashboardId?}"
|
||||
@implements IAsyncDisposable
|
||||
@inject Microsoft.Extensions.Configuration.IConfiguration Configuration
|
||||
@inject IOptions<AppSettings> AppSettingsOptions
|
||||
@inject NavigationManager Navigation
|
||||
@inject DashboardApiClient DashboardApi
|
||||
|
||||
@@ -45,8 +45,8 @@
|
||||
private string SelectedDashboardId { get; set; } = string.Empty;
|
||||
private string DashboardKey => $"{SelectedDashboardId}-{(IsDesigner ? "designer" : "viewer")}";
|
||||
|
||||
private string DashboardEndpoint => $"{Configuration["ApiBaseUrl"]?.TrimEnd('/')}/api/dashboard";
|
||||
private string HubEndpoint => $"{Configuration["ApiBaseUrl"]?.TrimEnd('/')}/hubs/dashboards";
|
||||
private string DashboardEndpoint => $"{AppSettingsOptions.Value.ApiBaseUrl.TrimEnd('/')}/api/dashboard";
|
||||
private string HubEndpoint => $"{AppSettingsOptions.Value.ApiBaseUrl.TrimEnd('/')}/hubs/dashboards";
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
|
||||
@@ -17,4 +17,6 @@
|
||||
@using DevExpress.Blazor
|
||||
@using DevExpress.DashboardBlazor
|
||||
@using DevExpress.DashboardWeb
|
||||
@using DevExpress.Data.Filtering
|
||||
@using DevExpress.Data.Filtering
|
||||
@using Microsoft.Extensions.Options
|
||||
@using DbFirst.BlazorWebApp
|
||||
@@ -1,3 +1,4 @@
|
||||
using DbFirst.BlazorWebApp;
|
||||
using DbFirst.BlazorWebApp.Components;
|
||||
using DbFirst.BlazorWebApp.Services;
|
||||
using DevExpress.Blazor;
|
||||
@@ -13,6 +14,7 @@ builder.Services.AddScoped<ThemeState>();
|
||||
builder.Services.AddScoped<BandLayoutService>();
|
||||
|
||||
var apiBaseUrl = builder.Configuration["ApiBaseUrl"];
|
||||
builder.Services.Configure<AppSettings>(builder.Configuration);
|
||||
void ConfigureClient(HttpClient client)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(apiBaseUrl))
|
||||
|
||||
Reference in New Issue
Block a user