简体   繁体   English

使用Nancy 2.0(clinteastwood)提供嵌入的静态文件

[英]Serving embeded static files with Nancy 2.0 (clinteastwood)

I'm trying to serve static assets in the folder assets via Nancy. 我正在尝试通过Nancy在文件夹assets提供静态资产。 Here's the code that I am using in the Bootstrapper: 这是我在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. 我已将文件(project root)/assets/test.css标记为嵌入式资源。 Yet when I start up the server and visit localhost:5000/assets/test.css I get a 404 error. 但是,当我启动服务器并访问localhost:5000/assets/test.css出现404错误。

It should be noted that I'm running dotnet core 2.0-preview2-final on OS X Sierra. 应该注意的是,我在OS X Sierra上运行dotnet core 2.0-preview2-final。

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. 我在Nancy Slack频道中与@inqonsole(Kevin Boon)进行了交谈,他让我尝试了一个在他的机器上工作的项目,该机器是一个不同的平台。 Still didn't work. 还是没用。 Probably a bug in Nancy-Embedded (I'll file it on their Github). 可能是Nancy-Embedded中的一个错误(我将其归档在他们的Github上)。 He suggested using Kestrel instead to serve the embedded files. 他建议改用Kestrel服务嵌入式文件。 This is in the Startup class: 这是在Startup类中:

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. 您需要在项目中具有来自NuGet的Microsoft.AspNetCore.StaticFilesMicrosoft.Extensions.FileProviders.Embedded软件包。

For the sake of completeness (for other dotnet core noobs like me), here's the Main function of Program.cs: 为了完整起见(对于像我这样的其他dotnet核心菜鸟),这是Program.cs的主要功能:

static void Main(string[] args)
    {
        IWebHost webHost = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .Build();
        webHost.Run();
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM