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
中 - 然后开启。