简体   繁体   English

.NET Core Nancy应用程序提供静态文件

[英].NET Core Nancy application serving static files

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 . 我正在尝试使用Nancy构建一个最小的可行网站作为.NET Core项目,其中一些后端处理和静态文件作为前端驻留在默认项目文件夹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. 主要问题是我不明白如何使应用程序响应静态文件,因为默认约定不适用于新的.NET Core项目系统。

Building Nancy applications as classic .NET Framework applications is well documented and there are many samples on the web on how to do it. 将Nancy应用程序构建为经典的.NET Framework应用程序已有详细记录,网上有很多关于如何执行此操作的示例。 But .NET Core projects ( .xproj ) differ a lot from classic .NET Framework projects ( .csproj ). 但.NET Core项目( .xproj )与传统的.NET Framework项目( .csproj )有很大不同。 I like the new project system a lot, but I don't understand how to integrate Nancy parts into it. 我很喜欢新的项目系统,但我不明白如何将Nancy部分集成到其中。 And there is a lack of documentation and samples on how to do it. 而且缺乏关于如何做的文档和样本。

TL;DR : GitHub repository , where the demo projects with all needed plumbing code resides. TL; DRGitHub存储库 ,其中包含所有需要的管道代码的演示项目所在。 For Nancy v. 1.4.3 as well as for prerelease v. 2.0.0-barneyrubble. 对于Nancy v.1.4.3以及对于预发布版本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. 支持.NET Core和.NET Standard的Nancy v.2仍然处于预发布状态,所以即使你想坚持使用稳定的v​​.1分支 - 没问题。

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 1)创建一个新的空ASP.NET核心Web应用程序

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: 2) (如果你想坚持Nancy "netcoreapp1.0"必须提供打开project.json ,删除"Microsoft.NETCore.App"依赖项并将目标框架从"netcoreapp1.0"更改为"net46" ,这样frameworks部分就会显示像那样:

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

3) Add the following dependencies to project.json: "Microsoft.AspNetCore.Owin" and "Nancy" . 3)将以下依赖项添加到project.json: "Microsoft.AspNetCore.Owin""Nancy" At the time of writing the dependencies section of project.json for v. 1: 在编写project.json的依赖项部分时为project.json

"dependencies": {
  // Ommited dependencies
  "Microsoft.AspNetCore.Owin": "1.0.0",
  "Nancy": "1.4.3"
},

For v. 2: 对于第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: 4)创建一个实现IRootPathProvider的类,并通过利用IHostingEnvironment对象在运行时提供wwwroot文件夹( WebRootPath属性)的路径:

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. 5)创建一个派生自DefaultNancyBootstrapper的类,它将检索IHostingEnvironment对象并将其传递给先前定义的Root Provider。 It will also change default StaticContentsConventions with your own: 它还将使用您自己的更改默认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) Open Startup class and replace the last line in Configure method with this one: 6)打开Startup类并用这一个替换Configure方法中的最后一行:

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

It leverages Bootstrapper created in the previous step. 它利用上一步中创建的Bootstrapper。

7) Now it's time to define your NancyModule . 7)现在是时候定义你的NancyModule V. 1: V. 1:

public class AppNancyModule : NancyModule
{
    public AppNancyModule()
    {
        Get["/"] = _ => View["index"];
        Get["/{fileName}"] = parameters => View[parameters.fileName];
    }
}

V. 2: 五.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. 只需将您的静态文件放在wwwroot - 然后关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM