gpt4 book ai didi

c# - NancyFX 中的静态内容与 ASP.Net Core

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:21 25 4
gpt4 key购买 nike

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

Nancy 约定是将静态内容存储在 /Content 中,而 ASP.Net Core 约定是将其存储在 /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");
});

但是当我访问这条路由时,Nancy 总是返回 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 的类。 Nancy 会在 Startup 上发现任何继承自此的类,因此您可以将它放在任何您想要的地方。

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

完成后,我就可以访问 /Content 中的静态内容。此外,我还能够通过添加一个继承自 DefaultNancyBootstrapper 的类来添加其他静态目录(例如,如果我想坚持使用 /wwwroot)。同样,Nancy 会在 Startup 上找到它,因此您可以将它放在任何地方。

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

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

关于c# - NancyFX 中的静态内容与 ASP.Net Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47665938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com