.NET Core Nancy Static File Application

I am trying to create a minimally viable website as a .NET Core project using Nancy with some backend processing and static files as the interface that is in the default project folder wwwroot . The main problem is that I don’t understand how to make the application respond with static files, because the default conventions do not apply to the new .NET Core project system.

Building Nancy applications as classic .NET Framework applications is well documented, and there are many examples on the Internet of how to do this. But .NET Core projects ( .xproj ) are very different from classic .NET Framework projects ( .csproj ). I like the new project system, but I don’t understand how to integrate Nancy's parts into it. And there is a lack of documentation and samples on how to do this.

+5
source share
1 answer

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.

+7
source

Source: https://habr.com/ru/post/1260531/


All Articles