我是靠谱客的博主 忧心宝马,最近开发中收集的这篇文章主要介绍使用.NET Core和NancyFX探索最小的WebAPI,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

In my last blog post I was exploring a minimal WebAPI with ASP.NET Core. In this one I wanted to look at how NancyFX does it. Nancy is an open source framework that takes some inspiration from Ruby's "Sinatra" framework (get it? Nancy Sinatra) and it's a great alternative to ASP.NET. It is an opinionated framework - and that's good thing. Nancy promotes what they call the "super-duper-happy-path." That means things should just work, they should be easy to customize, your code should be simple and Nancy itself shouldn't get in your way.

在我的上一篇博客文章中,我正在探索带有ASP.NET Core的最小WebAPI 。 在这篇文章中,我想看看NancyFX是如何做到的。 Nancy是一个开放源代码框架,它从Ruby的“ Sinatra”框架(得到它吗?Nancy Sinatra)中获得了一些启发,并且是ASP.NET的绝佳替代品。 这是一个自以为是的框架-那是一件好事。 南希推崇他们所谓的“超级骗子快乐路径”。 这意味着事情应该可以正常工作,应该易于定制,您的代码应该简单,而Nancy本身也不会妨碍您。

As I said, Nancy is open source and hosted on GitHub, so the code is here https://github.com/NancyFx/Nancy. They're working on a .NET Core version right now that is Nancy 2.0, but Nancy 1.x has worked great - and continues to - on .NET 4.6 on Windows. It's important to note that Nancy 1.4.3 is NOT beta and it IS in production.

就像我说的那样, Nancy是开源的,并托管在GitHub上,因此代码位于https://github.com/NancyFx/Nancy 。 他们现在正在使用.NET Core版本(即Nancy 2.0)进行工作,但是Nancy 1.x在Windows上的.NET 4.6上运行良好-并继续保持这种状态。 重要的是要注意,Nancy 1.4.3不是beta版,而是在生产中。

As of a few weeks ago there was a Beta of Nancy 2.0 on NuGet so I figured I'd do a little Hello Worlding and a Web API with Nancy on .NET Core. You should explore their samples in depth as they are not just more likely to be correct than my blog, they are just MORE.

几周前,NuGet上有一个Nancy 2.0 Beta,所以我认为我要在.NET Core上做一些Hello Worlding和Nancy的Web API。 您应该深入研究它们的样本,因为它们不仅比我的博客更正确,而且更多。

I wanted to host Nancy with the ASP.NET Core "Kestrel" web server. The project.json is simple, asking for just Kestrel, Nancy, and the Owin adapter for ASP.NET Core.

我想用ASP.NET Core“ Kestrel” Web服务器托管Nancy。 project.json很简单,只需要Kestrel,Nancy和用于ASP.NET Core的Owin适配器即可。

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.Owin": "1.0.0",
    "Nancy": "2.0.0-barneyrubble"
  },
  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },
  "frameworks": {
    "netcoreapp1.0": {}
  }
}

And the Main is standard ASP.NET Core preparation. setting up the WebHost and running it with Kestrel:

而主要的是标准的ASP.NET Core准备。 设置WebHost并使用Kestrel运行它:

using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace NancyApplication
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseStartup<Startup>()
.Build();

host.Run();
}
}
}

Startup tells ASP.NET Core via Owin that Nancy is in charge (and sure, didn't need to be its own file or it could have been in UseStartup in Main as a lambda)

Startup通过Owin告诉ASP.NET Core Nancy负责(当然,不需要是它自己的文件,或者它可以作为Lambda放在Main的UseStartup中)

using Microsoft.AspNetCore.Builder;
using Nancy.Owin;

namespace NancyApplication
{
   public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseOwin(x => x.UseNancy());
        }
   }
}

Here's where the fun stuff happens. Check out a simple Nancy Module.

这是有趣的事情发生的地方。 签出一个简单的Nancy模块。

using Nancy;
namespace NancyApplication
{
        public class HomeModule : NancyModule
    {
        public HomeModule()
        {
            Get("/", args => "Hello World, it's Nancy on .NET Core");
        }
    }
}

Then it's just "dotnet restore" and "dotnet run" and I'm in business. But let's do a little more. This little bit was largely stolen from Nancy's excellent samples repository. Here we've got another route that will respond to a GET to /test/Scott, then make and return a new Person() object. Since I'm going to pass in the Accept: application/json header I'll get JSON back.

那只是“ dotnet恢复”和“ dotnet运行”而已。 但是,让我们做更多。 这点大部分是从南希的出色样本库中窃取的。 在这里,我们有另一条路线,该路线将响应GET到/ test / Scott,然后生成并返回一个新的Person()对象。 由于我要传递Accept:application / json标头,因此我将返回JSON。

using Nancy;
 
namespace NancyApplication
{
    public class HomeModule : NancyModule
    {
        public HomeModule()
        {
            Get("/", args => "Hello from Nancy running on CoreCLR");
            Get("/test/{name}", args => new Person() { Name = args.name });
        }
    }

    public class Person
    {
        public string Name { get; set; }
    }
}

I'm using the Postman application to test this little Web API and you can see the JSON response below:

我正在使用Postman应用程序来测试这个小的Web API,您可以在下面看到JSON响应:

Postman shows a JSON object coming back from a GET request to a Web API

Nancy is a very complete and sophisticated framework with a LOT of great code to explore. It's extremely modular and flexible. It works with ASP.NET, with WCF, on Azure, with Owin, alongside Umbraco, with Mono, and so much more. I'm looking forward to exploring their .NET Core version as it continues development.

Nancy是一个非常完整和复杂的框架,其中包含许多值得探索的优秀代码。 它具有极高的模块化和灵活性。 它可以与ASP.NET,WCF,Azure,Owin,Umbraco,Mono等一起使用。 我期待着随着他们的.NET Core版本的不断发展。

Finally, if you're a new open source contributor or are considering being a First Timer and help out an open source project, you might find the idea of getting involved with such a sophisticated project intimidating. Nancy participates in UpForGrabs and has some issues that are marked as "up for grabs" that could be a good starter point where you could help out a deserving project AND get involved in open source.

最后,如果您是新的开源贡献者,或者正在考虑成为First Timer并帮助一个开源项目,则可能会发现参与如此复杂的项目的想法令人生畏。 南希(Nancy)参加了UpForGrabs,并有一些被标记为“抢手”的问题,这可能是一个很好的起点,您可以在该问题上帮助一个值得做的项目并参与开源。

* WoCTechChat photo used under CC

* CC下使用的WoCTechChat照片

翻译自: https://www.hanselman.com/blog/exploring-a-minimal-webapi-with-net-core-and-nancyfx

最后

以上就是忧心宝马为你收集整理的使用.NET Core和NancyFX探索最小的WebAPI的全部内容,希望文章能够帮你解决使用.NET Core和NancyFX探索最小的WebAPI所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部