[英]Static Content in NancyFX with ASP.Net Core
I'm using Nancy 2.0.0 with ASP.Net Core 2.0.0 and I can't get my application to return static content (in this case, a zip file) from a route defined in a Nancy module. 我正在使用Nancy 2.0.0与ASP.Net Core 2.0.0,我无法让我的应用程序从Nancy模块中定义的路由返回静态内容(在本例中为zip文件)。
The Nancy convention is to store static content in /Content
and the ASP.Net Core convention is to store it in /wwwroot
, but I can't get my app to recognize either. Nancy约定是将静态内容存储在
/Content
,ASP.Net Core约定是将其存储在/wwwroot
,但我无法让我的应用程序识别。
My Startup.Configure
method looks like this: 我的
Startup.Configure
方法如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseOwin(b => b.UseNancy());
}
And my module route looks like this: 我的模块路由如下所示:
Get("/big_file", _ => {
return Response.AsFile("wwwroot/test.zip");
});
But Nancy always returns a 404 when I hit this route. 但是当我点击这条路线时,南希总是返回404。 I've also tried directing ASP.Net Core to the static directory expected by Nancy, like this:
我也尝试将ASP.Net Core引导到Nancy期望的静态目录,如下所示:
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Content")),
RequestPath = new PathString("/Content")
});
But this didn't work either. 但这也不起作用。 I've tried putting the file in
/Content
and in /wwwroot
with the same result. 我已经尝试将文件放在
/Content
和/wwwroot
,结果相同。 And I've tried different casing of Content
, but nothing seems to work. 我尝试过不同的
Content
,但似乎没什么用。 What am I missing? 我错过了什么?
I figured it out. 我想到了。 The issue was that I needed to let Nancy know what I wanted to use as the root path for the application.
问题是我需要让Nancy知道我想用什么作为应用程序的根路径。 I did this by creating a class that inherits from
IRootPathProvider
. 我是通过创建一个继承自
IRootPathProvider
的类来IRootPathProvider
。 Nancy will discover any class that inherits from this on Startup, so you can put it wherever you want. Nancy将在Startup上发现任何继承此类的类,因此您可以将它放在任何您想要的位置。
public class DemoRootPathProvider : IRootPathProvider
{
public string GetRootPath()
{
return Directory.GetCurrentDirectory();
}
}
Once this was in place I was able to access static content in /Content
. 一旦到位,我就可以访问
/Content
静态/Content
。 Additionally, I was able to add additional static directories (for example, if I wanted to stick with /wwwroot
) by adding a class that inherits from DefaultNancyBootstrapper
. 此外,通过添加一个继承自
DefaultNancyBootstrapper
的类,我能够添加其他静态目录(例如,如果我想坚持使用/wwwroot
)。 Again, Nancy will find this on Startup, so you can put it anywhere. 再一次,Nancy会在Startup上找到它,所以你可以把它放在任何地方。
public class DemoBootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureConventions(NancyConventions conventions)
{
base.ConfigureConventions(conventions);
conventions.StaticContentsConventions.Add(
StaticContentConventionBuilder.AddDirectory("wwwroot")
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.