Skip to content

Feature Request: Loading content from EmbeddedResource in the assembly #31

Open

Description

@frg2089

Do we have plans to support loading content from EmbeddedResource in the assembly?

like this https://github.com/JBildstein/SpiderEye/blob/master/Source/SpiderEye.Core/Content/EmbeddedContentProvider.cs

Activity

  1. Lemonify commented on Dec 8, 2023

    @Lemonify
    Contributor

    Hi, I've got this somewhat implemented for my purposes, so if you still need it, I could share it or make a PR (seems like I am in the contributing mood lately 😁). There is a single drawback, though. Embedded files must be flattened, meaning that all JS/CSS/images must be in the same directory as index.html. It's fine by me, but it should be somehow possible to configure it to accept any folder structure.

  2. frg2089 commented on Dec 9, 2023

    @frg2089
    Author

    Hi, I've got this somewhat implemented for my purposes, so if you still need it, I could share it or make a PR (seems like I am in the contributing mood lately 😁). There is a single drawback, though. Embedded files must be flattened, meaning that all JS/CSS/images must be in the same directory as index.html. It's fine by me, but it should be somehow possible to configure it to accept any folder structure.

    I'm glad you implemented this feature. Would you mind sharing it?
    Normally EmbeddedResource uses . for resource names. We can change the value of LogicalName to use /.

  3. Lemonify commented on Dec 9, 2023

    @Lemonify
    Contributor

    It's super similar to the HostedContent class (and could be improved by making an abstract class or merging Hosted and Embedded classes together).

    Project file (.csproj) edits:
    Replace EmbeddedResource Include with your frontend dist/output folder. The only drawback is that you need to setup your frontend build process to output all files in a single directory, not nested like usual.

    <ItemGroup>
    	<EmbeddedResource Include="dist\**\*">
    		<LogicalName>%(Filename)%(Extension)</LogicalName>
    	</EmbeddedResource>
    </ItemGroup>
    

    Content Provider:

    public class EmbeddedContent : IWebviewContent
    {
        private readonly WebApplication _webApp;
    
        public EmbeddedContent(Assembly embeddedAssembly, int port = 0, bool activateLog = false, IDictionary<string, string>? additionalMimeTypes = null)
        {
            WebApplicationBuilder builder = WebApplication.CreateBuilder();
            builder.WebHost.UseKestrel(options => {
                options.Listen(IPAddress.Loopback, port);
            });
    
            if (!activateLog)
            {
                builder.Logging.ClearProviders();
            }
    
            _webApp = builder.Build();
    
            FileServerOptions fileServerOptions = new()
            {
                FileProvider = new EmbeddedFileProvider(embeddedAssembly, ""),
                RequestPath = "",
                EnableDirectoryBrowsing = true
            };
    
            if (additionalMimeTypes != null)
            {
                FileExtensionContentTypeProvider extensionProvider = new();
    
                foreach (var mimeType in additionalMimeTypes)
                {
                    extensionProvider.Mappings.Add(mimeType);
                }
    
                fileServerOptions.StaticFileOptions.ContentTypeProvider = extensionProvider;
            }
    
            _webApp.UseFileServer(fileServerOptions);
            _webApp.Start();
        }
    
        public string ToWebviewUrl()
        {
            return _webApp.Urls.First();
        }
    }
    

    How to use it with Webview:

    IWebviewContent content = new EmbeddedContent(typeof(Program).Assembly);
    webview.Navigate(content).Run();
    
  4. tbmale commented on Jun 4, 2026

    @tbmale

    I would also like to see this feature!
    I think it would fairly easy to swap the current file provider with a CompositeFileProvider as in this microsoft example:

    var physicalProvider = _env.ContentRootFileProvider;
    var manifestEmbeddedProvider = 
        new ManifestEmbeddedFileProvider(typeof(Program).Assembly);
    var compositeProvider = 
        new CompositeFileProvider(physicalProvider, manifestEmbeddedProvider);
    
    services.AddSingleton<IFileProvider>(compositeProvider);
    

    @geaz : Does this worth a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions