.NET Core Nancy application serving static files

折月煮酒 提交于 2021-02-08 15:13:51

问题


I am trying to build a minimal viable web site as a .NET Core project using Nancy with some backend processing and static files as frontend which resides in default project folder wwwroot. The main problem is I don't understand how to make the app respond with static files, because default conventions don't apply to the new .NET Core project system.

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


回答1:


TL;DR: GitHub repository, where the demo projects with all needed plumbing code resides. For Nancy v. 1.4.3 as well as for prerelease v. 2.0.0-barneyrubble.

Nancy v. 2, which supports .NET Core and .NET Standard is still in prerelease state, so even if you would like to stick with stable v. 1 branch - no problem.

Here's a step-by-step on how to do it from the scratch, which worked for me:

1) Create a new Empty ASP.NET Core Web Application

2) (Mandatory if you would like to stick with Nancy v. 1) Open project.json, remove "Microsoft.NETCore.App" dependency and change target framework from "netcoreapp1.0" to "net46", so frameworks section would look like that:

"frameworks": {
    "net46": {}
},

3) Add the following dependencies to project.json: "Microsoft.AspNetCore.Owin" and "Nancy". At the time of 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 implementing IRootPathProvider and will provide a path to your wwwroot folder (WebRootPath property) at runtime by utilizing 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, which will retrieve IHostingEnvironment object and pass it to the previously defined Root Provider. It will also change default StaticContentsConventions with 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 Startup class and replace the last line in Configure method with this one:

app.UseOwin(x => x.UseNancy(options => options.Bootstrapper = new AppNancyBootstrapper(env)));

It leverages 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 to go! Just place your static files in wwwroot - and fire off.



来源:https://stackoverflow.com/questions/40870618/net-core-nancy-application-serving-static-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!