support for appsettings.json in Maui app
This commit is contained in:
parent
a72e5e02da
commit
edf955f4cd
@ -7,15 +7,17 @@ using Oqtane.Services;
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Windows.Storage.Provider;
|
using Windows.Storage.Provider;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
namespace Oqtane.Maui;
|
namespace Oqtane.Maui;
|
||||||
|
|
||||||
public static class MauiProgram
|
public static class MauiProgram
|
||||||
{
|
{
|
||||||
// the API service url
|
// the API service url - can be overridden in an appsettings.json in AppDataDirectory
|
||||||
//static string apiurl = "https://www.dnfprojects.com/"; // for testing remote site
|
|
||||||
static string apiurl = "http://localhost:44357/"; // for local development (Oqtane.Server must be already running for MAUI client to connect)
|
static string apiurl = "http://localhost:44357/"; // for local development (Oqtane.Server must be already running for MAUI client to connect)
|
||||||
//static string apiurl = "http://localhost:44357/sitename/"; // local microsite example
|
//static string apiurl = "http://localhost:44357/sitename/"; // local microsite example
|
||||||
|
//static string apiurl = "https://www.dnfprojects.com/"; // for testing remote site
|
||||||
|
|
||||||
public static MauiApp CreateMauiApp()
|
public static MauiApp CreateMauiApp()
|
||||||
{
|
{
|
||||||
@ -29,7 +31,9 @@ public static class MauiProgram
|
|||||||
builder.Services.AddMauiBlazorWebView();
|
builder.Services.AddMauiBlazorWebView();
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
builder.Services.AddBlazorWebViewDeveloperTools();
|
builder.Services.AddBlazorWebViewDeveloperTools();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
LoadAppSettings();
|
||||||
|
|
||||||
var httpClient = new HttpClient { BaseAddress = new Uri(GetBaseUrl(apiurl)) };
|
var httpClient = new HttpClient { BaseAddress = new Uri(GetBaseUrl(apiurl)) };
|
||||||
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Shared.Constants.MauiUserAgent);
|
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Shared.Constants.MauiUserAgent);
|
||||||
@ -62,6 +66,28 @@ public static class MauiProgram
|
|||||||
return builder.Build();
|
return builder.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void LoadAppSettings()
|
||||||
|
{
|
||||||
|
// appsettings.json file format
|
||||||
|
// {
|
||||||
|
// "Url": "http://localhost:44357/"
|
||||||
|
// }
|
||||||
|
|
||||||
|
string file = Path.Combine(FileSystem.Current.AppDataDirectory, "appsettings.json");
|
||||||
|
if (File.Exists(file))
|
||||||
|
{
|
||||||
|
using FileStream stream = File.OpenRead(file);
|
||||||
|
using StreamReader reader = new StreamReader(stream);
|
||||||
|
var content = reader.ReadToEnd();
|
||||||
|
var obj = JsonSerializer.Deserialize<JsonObject>(content)!;
|
||||||
|
if (!string.IsNullOrEmpty((string)obj["Url"]))
|
||||||
|
{
|
||||||
|
apiurl = (string)obj["Url"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void LoadClientAssemblies(HttpClient http)
|
private static void LoadClientAssemblies(HttpClient http)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
Loading…
x
Reference in New Issue
Block a user