TL; DR:GitHub repository,其中包含所有需要的管道代碼的演示項目駐留。對於南希訴1.4.3以及預發佈訴2.0.0-barneyrubble。支持.NET Core和.NET Standard的Nancy v.2仍然處於預發佈狀態,所以即使您想堅持穩定的v.1分支 - 沒有問題。
下面是關於如何從劃痕,這爲我工作做一步一步:
1)新建一個空的ASP.NET核心Web應用程序
2)(強制性的,如果你想堅持與南希V1。)打開project.json
,請從"netcoreapp1.0"
"Microsoft.NETCore.App"
依賴和改變目標框架"net46"
,所以frameworks
部分看起來就像是:
"frameworks": {
"net46": {}
},
3)將以下依賴項添加到project.json:"Microsoft.AspNetCore.Owin"
和"Nancy"
。在乞伏編寫的project.json
依賴關係部分的時間1:
"dependencies": {
// Ommited dependencies
"Microsoft.AspNetCore.Owin": "1.0.0",
"Nancy": "1.4.3"
},
欲訴2:
"dependencies": {
// Ommited dependencies
"Microsoft.AspNetCore.Owin": "1.0.0",
"Nancy": "2.0.0-barneyrubble"
},
4)創建實施IRootPathProvider
一類,並且將提供給您的wwwroot
文件夾的路徑(WebRootPath
屬性)在運行時通過利用IHostingEnvironment
對象:
public class AppRootPathProvider : IRootPathProvider
{
private readonly IHostingEnvironment _environment;
public AppRootPathProvider(IHostingEnvironment environment)
{
_environment = environment;
}
public string GetRootPath()
{
return _environment.WebRootPath;
}
}
5)創建交流衍生自DefaultNancyBootstrapper
的lass,其將檢索IHostingEnvironment
對象並將其傳遞給先前定義的根提供者。它也將改變默認StaticContentsConventions
用自己:
public class AppNancyBootstrapper : DefaultNancyBootstrapper
{
public AppNancyBootstrapper(IHostingEnvironment environment)
{
RootPathProvider = new AppRootPathProvider(environment);
}
protected override void ConfigureConventions(NancyConventions conventions)
{
base.ConfigureConventions(conventions);
conventions.StaticContentsConventions.AddDirectory("css");
conventions.StaticContentsConventions.AddDirectory("js");
conventions.StaticContentsConventions.AddDirectory("fonts");
}
protected override IRootPathProvider RootPathProvider { get; }
}
6)打開Startup
類,並與這一個替換最後一行Configure
方法:
app.UseOwin(x => x.UseNancy(options => options.Bootstrapper = new AppNancyBootstrapper(env)));
它利用引導程序在上一步中創建。
7)現在是時候定義您的NancyModule
。 V.1:
public class AppNancyModule : NancyModule
{
public AppNancyModule()
{
Get["/"] = _ => View["index"];
Get["/{fileName}"] = parameters => View[parameters.fileName];
}
}
V. 2:
public class AppNancyModule : NancyModule
{
public AppNancyModule()
{
Get("/", _ => View["index"]);
Get("/{fileName}", parameters => View[parameters.fileName]);
}
}
,你是好去!只需將您的靜態文件放在wwwroot
中 - 然後開啓。