'How to create and use a UI from a package

I'm creating a .net core c# package to manage application settings via SQL. Simple enough task, but I also need an interface for these settings. This package will be consumed by several different projects all with their own settings, and so I'm trying to find out how to implement a UI that's within my settings package but is available from the projects referencing that package. Very much like how Swagger works.

So once I've built I can just do something like:

app.UseSettingsUI(*some parameters*);

And I'll have a UI that can edit the settings in each project at some predefined referential URL.

I don't even know what to Google to find out any starting block.

For clarification; I can create the UI, the service, settings etc. It's just how to include a UI from another package that I can't figure out.



Solution 1:[1]

So I figured this out for myself:

Basically, you just need to tell the hosting environment about your interface.

First off you can extend the WebApplicationBuilder

public static WebApplicationBuilder UseMyApp(this WebApplicationBuilder builder)

and add all the services that you may need,as well as any options you may require such as

builder.Services.AddServerSideBlazor();

But you also need to tell it about your assembly, which is a one liner:

builder.Services.AddRazorPages().PartManager.ApplicationParts.Add(new AssemblyPart(ThisAssembly));

ThisAssembly is just a pointer to an assembly in the interfaces project:

ThisAssembly => Assembly.GetAssembly(typeof(<some poco>))!;

This adds all the code from the Razor pages to the host application.

Then you need to extend the WebApplication:

public static void UseMyAppsUI(this WebApplication app)

if your adding your interface to an API you may want to add all the usual mapping and routing to this extension:

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.UseRouting();

but to get your app pages working they'll need their own static files:

app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new EmbeddedFileProvider(ThisAssembly, "MyApp.wwwroot")
    });

which adds the wwwroot files from your project into the Host Application.

Then to utilise your project in another in the Host program.cs just add:

builder.UseMyApp();
...
app.UseMyAppsUI();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tod