improve appsettings.json in Maui client
This commit is contained in:
parent
d518860a34
commit
5be640632b
|
@ -1,18 +1,52 @@
|
|||
<DynamicComponent Type="@ComponentType" Parameters="@Parameters"></DynamicComponent>
|
||||
@using System.Text.Json;
|
||||
@using System.Text.Json.Nodes;
|
||||
|
||||
@code {
|
||||
Type ComponentType = Type.GetType("Oqtane.App, Oqtane.Client");
|
||||
private IDictionary<string, object> Parameters { get; set; }
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Parameters = new Dictionary<string, object>();
|
||||
Parameters.Add(new KeyValuePair<string, object>("AntiForgeryToken", ""));
|
||||
Parameters.Add(new KeyValuePair<string, object>("Runtime", "Hybrid"));
|
||||
Parameters.Add(new KeyValuePair<string, object>("RenderMode", "Hybrid"));
|
||||
Parameters.Add(new KeyValuePair<string, object>("VisitorId", -1));
|
||||
Parameters.Add(new KeyValuePair<string, object>("RemoteIPAddress", ""));
|
||||
Parameters.Add(new KeyValuePair<string, object>("AuthorizationToken", ""));
|
||||
}
|
||||
@if (string.IsNullOrEmpty(message))
|
||||
{
|
||||
<DynamicComponent Type="@ComponentType" Parameters="@Parameters"></DynamicComponent>
|
||||
}
|
||||
else
|
||||
{
|
||||
<br /><br /><center>@message</center>
|
||||
}
|
||||
|
||||
@code {
|
||||
Type ComponentType = Type.GetType("Oqtane.App, Oqtane.Client");
|
||||
private IDictionary<string, object> Parameters { get; set; }
|
||||
private string message = "";
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Parameters = new Dictionary<string, object>();
|
||||
Parameters.Add(new KeyValuePair<string, object>("AntiForgeryToken", ""));
|
||||
Parameters.Add(new KeyValuePair<string, object>("Runtime", "Hybrid"));
|
||||
Parameters.Add(new KeyValuePair<string, object>("RenderMode", "Hybrid"));
|
||||
Parameters.Add(new KeyValuePair<string, object>("VisitorId", -1));
|
||||
Parameters.Add(new KeyValuePair<string, object>("RemoteIPAddress", ""));
|
||||
Parameters.Add(new KeyValuePair<string, object>("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<JsonObject>(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";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
13
Oqtane.Maui/MauiConstants.cs
Normal file
13
Oqtane.Maui/MauiConstants.cs
Normal file
|
@ -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;
|
||||
}
|
|
@ -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<JsonObject>(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<JsonObject>(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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user