.NET Core Nancy应用程序提供静态文件 [英] .NET Core Nancy application serving static files

查看:48
本文介绍了.NET Core Nancy应用程序提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Nancy构建一个最小可行的网站作为.NET Core项目,并带有一些后端处理和静态文件作为前端,该文件位于默认项目文件夹 wwwroot 中.主要问题是我不了解如何使应用程序响应静态文件,因为默认约定不适用于新的.NET Core项目系统.

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.

将Nancy应用程序构建为经典的.NET Framework应用程序已有很好的文档记录,并且网络上有许多示例说明了如何执行此操作.但是.NET Core项目( .xproj )与经典.NET Framework项目( .csproj )有很大不同.我非常喜欢新的项目系统,但是我不知道如何将Nancy的部件集成到其中.而且缺少有关如何执行此操作的文档和示例.

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.

推荐答案

TL; DR : GitHub存储库,其中包含所有必需的管道代码的演示项目.对于Nancy 1.4.3版和预发行版2.0.0-barneyrubble.

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.

支持.NET Core和.NET Standard的Nancy v.2仍处于预发行状态,因此即使您希望坚持使用稳定的v​​.1分支-也不成问题.

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.

这是逐步解决问题的方法,对我有用:

1)创建一个新的空ASP.NET Core Web应用程序

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

2)(如果您想坚持使用Nancy v.1,则必须这样做)打开 project.json ,删除"Microsoft.NETCore.App" 依赖项,并将目标框架从"netcoreapp1.0" 更改为"net46" ,因此 frameworks 部分如下所示:

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)将以下依赖项添加到project.json:"Microsoft.AspNetCore.Owin" "Nancy" .在编写v.1的 project.json 的依赖项部分时:

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"
},

对于v.2:

"dependencies": {
  // Ommited dependencies
  "Microsoft.AspNetCore.Owin": "1.0.0",
  "Nancy": "2.0.0-barneyrubble"
},

4)创建一个实现 IRootPathProvider 的类,并在运行时使用提供指向您的 wwwroot 文件夹( WebRootPath 属性)的路径.> IHostingEnvironment 对象:

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)创建一个从 DefaultNancyBootstrapper 派生的类,该类将检索 IHostingEnvironment 对象并将其传递给先前定义的Root Provider.还将使用您自己的默认默认代码更改 StaticContentsConventions :

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)打开 Startup 类,并用以下内容替换 Configure 方法中的最后一行:

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)));

它利用了上一步中创建的Bootstrapper.

It leverages Bootstrapper created in the previous step.

7)现在是时候定义您的 NancyModule 了.V. 1:

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]);
    }
}

你很好!只需将您的静态文件放在 wwwroot 中,然后启动即可.

And you are good to go! Just place your static files in wwwroot - and fire off.

这篇关于.NET Core Nancy应用程序提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆