TL DR : GitHub repository where demo projects with all the necessary plumbing code are located. For Nancy v. 1.4.3, as well as for preliminary verification of version 2.0.0-barneyrubble.
Nancy v. 2, which supports .NET Core and .NET Standard, is still in a preliminary state, so even if you want to stick to the stable v branch. 1, no problem.
Here's a step-by-step guide on how to do this from scratch, which worked for me :
1) Create a new empty ASP.NET Core web application
2) (Required if you want to stick with Nancy v. 1) Open project.json , remove the dependency "Microsoft.NETCore.App" and change the target structure from "netcoreapp1.0" to "net46" , so the frameworks will look like this:
"frameworks": { "net46": {} },
3) Add the following dependencies to project.json: "Microsoft.AspNetCore.Owin" and "Nancy" . While writing the dependencies section of project.json for v. 1:
"dependencies": { // Ommited dependencies "Microsoft.AspNetCore.Owin": "1.0.0", "Nancy": "1.4.3" },
For v. 2:
"dependencies": { // Ommited dependencies "Microsoft.AspNetCore.Owin": "1.0.0", "Nancy": "2.0.0-barneyrubble" },
4) Create a class that implements IRootPathProvider , and provide the path to your wwwroot folder ( WebRootPath property) at runtime using the IHostingEnvironment object:
public class AppRootPathProvider : IRootPathProvider { private readonly IHostingEnvironment _environment; public AppRootPathProvider(IHostingEnvironment environment) { _environment = environment; } public string GetRootPath() { return _environment.WebRootPath; } }
5) Create a class derived from DefaultNancyBootstrapper that will retrieve the IHostingEnvironment object and pass it to the previously defined root provider. It will also change the default StaticContentsConventions to your own:
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) Open the Startup class and replace the last line in the Configure method as follows:
app.UseOwin(x => x.UseNancy(options => options.Bootstrapper = new AppNancyBootstrapper(env)));
It uses the Bootstrapper created in the previous step.
7) Now it's time to define your 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]); } }
And you are good! Just put your static files in wwwroot - and run.