如何在毛伊岛启动时从appsettings.json加载APP配置? [英] How to load app configuration from appsettings.json in MAUI startup?
本文介绍了如何在毛伊岛启动时从appsettings.json加载APP配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从毛伊应用程序中的appsettings.json文件中检索我的应用程序设置。 我将其标记为MauiAsset生成操作,我可以在生成的APK的的资产目录中看到它。 它似乎在ConfigureAppConfiguration中不可用,并且不存在用于将其添加到应用程序生成器中的AddJsonFile扩展。
我是否应该使用其他生成操作?
检索它的好方法是什么?
public void Configure(IAppHostBuilder appBuilder) { appBuilder .ConfigureAppConfiguration((app, config) => { // I should be able to work with appsettings here }) .ConfigureServices(svcs => { }) .UseMauiApp<App>(); }
推荐答案
可以使用MauiAppBuilder.Host
Getter获取常用的主机生成器。
var builder = MauiApp.CreateBuilder();
builder
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
})
.UseMauiApp<App>()
.Host
.ConfigureAppConfiguration((app, config) =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureServices((ctx, svcs) =>
{
})
.ConfigureLogging(logging =>
{
logging.AddSerilog();
});
若要获取应用程序位置和应用程序路径,请使用以下命令:
Assembly CallingAssembly = Assembly.GetEntryAssembly();
Version VersionRuntime = CallingAssembly.GetName().Version;
string AssemblyLocation = Path.GetDirectoryName(CallingAssembly.Location);
string ConfigFile = Path.Combine(AssemblyLocation, "appsettings.json");
这篇关于如何在毛伊岛启动时从appsettings.json加载APP配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文