0

I'm trying to serve static assets in the folder assets via Nancy. Here's the code that I am using in the Bootstrapper:

protected override void ConfigureConventions(NancyConventions nancyConventions)
{
    var assets = EmbeddedStaticContentConventionBuilder.AddDirectory(
        "/assets", GetType().Assembly);
    nancyConventions.StaticContentsConventions.Add(assets);
    base.ConfigureConventions(nancyConventions);
}

I have marked the file (project root)/assets/test.css as an embedded resource. Yet when I start up the server and visit localhost:5000/assets/test.css I get a 404 error.

It should be noted that I'm running dotnet core 2.0-preview2-final on OS X Sierra.

eltiare
  • 1,817
  • 1
  • 20
  • 28

1 Answers1

1

I spoke with @inqonsole (Kevin Boon) in the Nancy Slack channel, he had me try out a project that worked on his machine which is a different platform. Still didn't work. Probably a bug in Nancy-Embedded (I'll file it on their Github). He suggested using Kestrel instead to serve the embedded files. This is in the Startup class:

public void Configure(IApplicationBuilder app)
    {

        app.UseStaticFiles(new StaticFileOptions()
        {
            FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly, typeof(Startup).Namespace + ".assets"),
             RequestPath = new PathString("/assets")
        });

    }

You'll need to have the packages Microsoft.AspNetCore.StaticFiles and Microsoft.Extensions.FileProviders.Embedded from NuGet in your project.

For the sake of completeness (for other dotnet core noobs like me), here's the Main function of Program.cs:

static void Main(string[] args)
    {
        IWebHost webHost = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .Build();
        webHost.Run();
    } 
eltiare
  • 1,817
  • 1
  • 20
  • 28