[英]Static Content in NancyFX with ASP.Net Core
我正在使用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");
});
但是當我點擊這條路線時,南希總是返回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
的類來IRootPathProvider
。 Nancy將在Startup上發現任何繼承此類的類,因此您可以將它放在任何您想要的位置。
public class DemoRootPathProvider : IRootPathProvider
{
public string GetRootPath()
{
return Directory.GetCurrentDirectory();
}
}
一旦到位,我就可以訪問/Content
靜態/Content
。 此外,通過添加一個繼承自DefaultNancyBootstrapper
的類,我能夠添加其他靜態目錄(例如,如果我想堅持使用/wwwroot
)。 再一次,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.