具有 ASP.Net 核心的 NancyFX 中的静态内容

Static Content in NancyFX with ASP.Net Core

我将 Nancy 2.0.0 与 ASP.Net Core 2.0.0 一起使用,但我无法将我的应用程序从 return 静态内容(在本例中为 zip 文件) Nancy 模块中定义的路由。

Nancy 约定是将静态内容存储在 /Content 中,ASP.Net 核心约定是将其存储在 /wwwroot 中,但我的应用程序无法识别.

我的 Startup.Configure 方法如下所示:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();            
    app.UseOwin(b => b.UseNancy());
}

我的模块路由是这样的:

Get("/big_file", _ => {
    return Response.AsFile("wwwroot/test.zip");
});

但南希总是 return 当我走这条路线时,她是 404。我也试过将 ASP.Net Core 定向到 Nancy 期望的静态目录,如下所示:

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Content")),
    RequestPath = new PathString("/Content")
});

但这也没有用。我试过将文件放在 /Content/wwwroot 中,结果相同。我尝试了 Content 的不同大小写,但似乎没有任何效果。我错过了什么?

我明白了。问题是我需要让 Nancy 知道我想将什么用作应用程序的根路径。为此,我创建了一个继承自 IRootPathProvider 的 class。 Nancy 会在启动时发现任何继承自此的 class,因此您可以将它放在任何您想要的地方。

public class DemoRootPathProvider : IRootPathProvider
{
  public string GetRootPath()
  {
    return Directory.GetCurrentDirectory();
  }
}

完成后,我就可以访问 /Content 中的静态内容。此外,我可以通过添加继承自 DefaultNancyBootstrapper 的 class 来添加额外的静态目录(例如,如果我想坚持使用 /wwwroot)。同样,Nancy 会在启动时找到它,因此您可以将它放在任何地方。

public class DemoBootstrapper : DefaultNancyBootstrapper
{
  protected override void ConfigureConventions(NancyConventions conventions)
  {
    base.ConfigureConventions(conventions);

    conventions.StaticContentsConventions.Add(
        StaticContentConventionBuilder.AddDirectory("wwwroot")
    );
  }
}