diff --git a/Oqtane.Maui/Main.razor b/Oqtane.Maui/Main.razor index d139d631..f8b8069c 100644 --- a/Oqtane.Maui/Main.razor +++ b/Oqtane.Maui/Main.razor @@ -1,18 +1,52 @@ - +@using System.Text.Json; +@using System.Text.Json.Nodes; -@code { - Type ComponentType = Type.GetType("Oqtane.App, Oqtane.Client"); - private IDictionary Parameters { get; set; } - - protected override void OnInitialized() - { - Parameters = new Dictionary(); - Parameters.Add(new KeyValuePair("AntiForgeryToken", "")); - Parameters.Add(new KeyValuePair("Runtime", "Hybrid")); - Parameters.Add(new KeyValuePair("RenderMode", "Hybrid")); - Parameters.Add(new KeyValuePair("VisitorId", -1)); - Parameters.Add(new KeyValuePair("RemoteIPAddress", "")); - Parameters.Add(new KeyValuePair("AuthorizationToken", "")); - } +@if (string.IsNullOrEmpty(message)) +{ + +} +else +{ +

@message
+} + +@code { + Type ComponentType = Type.GetType("Oqtane.App, Oqtane.Client"); + private IDictionary Parameters { get; set; } + private string message = ""; + + protected override void OnInitialized() + { + Parameters = new Dictionary(); + Parameters.Add(new KeyValuePair("AntiForgeryToken", "")); + Parameters.Add(new KeyValuePair("Runtime", "Hybrid")); + Parameters.Add(new KeyValuePair("RenderMode", "Hybrid")); + Parameters.Add(new KeyValuePair("VisitorId", -1)); + Parameters.Add(new KeyValuePair("RemoteIPAddress", "")); + Parameters.Add(new KeyValuePair("AuthorizationToken", "")); + + if (MauiConstants.UseAppSettings) + { + 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(content)!; + if (string.IsNullOrEmpty((string)obj["Url"]) && string.IsNullOrEmpty(MauiConstants.ApiUrl)) + { + message = "You Must Set The Url In Either MauiConstants.cs Or " + file; + } + } + } + else + { + if (string.IsNullOrEmpty(MauiConstants.ApiUrl)) + { + message = "You Must Set The Url In MauiConstants.cs"; + } + } + } } diff --git a/Oqtane.Maui/MauiConstants.cs b/Oqtane.Maui/MauiConstants.cs new file mode 100644 index 00000000..7ff82bbb --- /dev/null +++ b/Oqtane.Maui/MauiConstants.cs @@ -0,0 +1,13 @@ +namespace Oqtane.Maui; + +public static class MauiConstants +{ + // the API service url (used as fallback if not set in appsettings.json) + public static string ApiUrl = ""; + //public static string ApiUrl = "http://localhost:44357/"; // for local development (Oqtane.Server must be already running for MAUI client to connect) + //public static string apiurl = "http://localhost:44357/sitename/"; // local microsite example + //public static string apiurl = "https://www.dnfprojects.com/"; // for testing remote site + + // specify if you wish to allow users to override the url via appsettings.json in the AppDataDirectory + public static bool UseAppSettings = true; +} diff --git a/Oqtane.Maui/MauiProgram.cs b/Oqtane.Maui/MauiProgram.cs index 3ff3b5bb..95a42117 100644 --- a/Oqtane.Maui/MauiProgram.cs +++ b/Oqtane.Maui/MauiProgram.cs @@ -6,19 +6,12 @@ using Oqtane.Modules; using Oqtane.Services; using System.Globalization; using System.Text.Json; -using Windows.Storage.Provider; using System.Text.Json.Nodes; namespace Oqtane.Maui; public static class MauiProgram { - // the API service url - can be overridden in an appsettings.json in AppDataDirectory - - 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 = "https://www.dnfprojects.com/"; // for testing remote site - public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); @@ -33,16 +26,19 @@ public static class MauiProgram builder.Services.AddBlazorWebViewDeveloperTools(); #endif - LoadAppSettings(); + var apiurl = LoadAppSettings(); - var httpClient = new HttpClient { BaseAddress = new Uri(GetBaseUrl(apiurl)) }; - httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Shared.Constants.MauiUserAgent); - httpClient.DefaultRequestHeaders.Add(Shared.Constants.MauiAliasPath, GetUrlPath(apiurl).Replace("/", "")); - builder.Services.AddSingleton(httpClient); - builder.Services.AddHttpClient(); // IHttpClientFactory for calling remote services via RemoteServiceBase + if (!string.IsNullOrEmpty(apiurl)) + { + var httpClient = new HttpClient { BaseAddress = new Uri(GetBaseUrl(apiurl)) }; + httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Shared.Constants.MauiUserAgent); + httpClient.DefaultRequestHeaders.Add(Shared.Constants.MauiAliasPath, GetUrlPath(apiurl).Replace("/", "")); + builder.Services.AddSingleton(httpClient); + builder.Services.AddHttpClient(); // IHttpClientFactory for calling remote services via RemoteServiceBase - // dynamically load client assemblies - LoadClientAssemblies(httpClient); + // dynamically load client assemblies + LoadClientAssemblies(httpClient, apiurl); + } // register localization services builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); @@ -67,28 +63,36 @@ public static class MauiProgram } - private static void LoadAppSettings() + private static string LoadAppSettings() { - // appsettings.json file format - // { - // "Url": "http://localhost:44357/" - // } - - string file = Path.Combine(FileSystem.Current.AppDataDirectory, "appsettings.json"); - if (File.Exists(file)) + var url = MauiConstants.ApiUrl; + if (MauiConstants.UseAppSettings) { - using FileStream stream = File.OpenRead(file); - using StreamReader reader = new StreamReader(stream); - var content = reader.ReadToEnd(); - var obj = JsonSerializer.Deserialize(content)!; - if (!string.IsNullOrEmpty((string)obj["Url"])) + string file = Path.Combine(FileSystem.Current.AppDataDirectory, "appsettings.json"); + if (File.Exists(file)) { - apiurl = (string)obj["Url"]; + using FileStream stream = File.OpenRead(file); + using StreamReader reader = new StreamReader(stream); + var content = reader.ReadToEnd(); + var obj = JsonSerializer.Deserialize(content)!; + if (!string.IsNullOrEmpty((string)obj["Url"])) + { + url = (string)obj["Url"]; + } + } + else + { + // create template appsettings.json file + using (StreamWriter writer = File.CreateText(file)) + { + writer.WriteLine("{ \"Url\": \"\" }"); + } } } + return url; } - private static void LoadClientAssemblies(HttpClient http) + private static void LoadClientAssemblies(HttpClient http, string apiurl) { try { @@ -227,7 +231,7 @@ public static class MauiProgram } catch (Exception ex) { - Debug.WriteLine($"Oqtane Error: Loading Client Assemblies {ex}"); + Debug.WriteLine($"Error Loading Client Assemblies From {apiurl} - {ex}"); } }