Scott Hanselman

Exploring a minimal WebAPI with .NET Core and NancyFX

July 30, '16 Comments [16] Posted in Open Source
Sponsored By

WOCinTechChat photo used under CCIn 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.

As I said, Nancy is open source and hosted on GitHub, so the code is here 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.

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.

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.

  "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:

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


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)

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.

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.

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

    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 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.

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.

* WoCTechChat photo used under CC

Sponsor: Thanks to Aspose for sponsoring the feed this week! Aspose makes programming APIs for working with files, like: DOC, XLS, PPT, PDF and countless more.  Developers can use their products to create, convert, modify, or manage files in almost any way. Aspose is a good company and they offer solid products. Check them out, and download a free evaluation!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Saturday, 30 July 2016 15:12:00 UTC
Thanks for the blog post Scott!

If anyone has any questions about Nancy feel free to jump onto our Slack channel, raise a Github issue or ping us @NancyFX on twitter.

We'd love more people testing our v2 (now on .Net Core) and more people submitting PRs and contributing to Nancy.

I started with Nancy in 2012, not knowing Git and was helped out by the Nancy team to learn it. We try to be as helpful and accommodating to seasoned pros and beginners.

Come and try Nancy and see all the fun you're missing =)

Saturday, 30 July 2016 15:16:47 UTC
We also have a Kestrel "Hello World" example here that uses the new ASP.Net Core Configuration to demonstrate its functionality as well as Nancy.
Saturday, 30 July 2016 21:37:59 UTC
Hi Scott,

Thanks for the article! :-)

I just wonder if WebAPI is going to have an upgraded version for ASP.NET Core, something like WebApi 3.0?

Cheers, Sia
Saturday, 30 July 2016 23:40:18 UTC

I suggest you read Scott's previous post. It explains that in .NET Core view and api controllers use the same base class. In other words webapi 3.0 is already in .net core.
Sunday, 31 July 2016 17:48:46 UTC
Seems like a variant of this package.json/*.cs are in the latest master branch of generator-aspnet for yeoman but they haven't released that to npm yet.

Note this package.json won't work with dotnet.exe 1.0.0-preview1-002702. So update your dotnet.exe to avoid headaches.
Paul Miller
Sunday, 31 July 2016 17:52:13 UTC
@paul Yup I updated the Yeoman ASPNet Generator the other day. Hopefully Nancy will be an option on next release which should be soon I hope =)

Yeoman ASPNet Generator link here for others
Monday, 01 August 2016 02:04:57 UTC
Why does webAPI get the spotlight instead of a much easier NancyFx? Why do vendors push a more costly to develop and maintain rpc?

Monday, 01 August 2016 06:59:43 UTC
@Ted - I haven't seen anybody pushing anything. Easier according to who? If you know WebAPI and know nothing about Nancy then that is subjective. Do you spend time learning Nancy to do something you already know how to do in WebAPI, what is more costly then?
Wednesday, 03 August 2016 12:05:03 UTC
I think I'm crying. It's that appealing.
Minimal work you have here.
Wednesday, 03 August 2016 16:47:41 UTC

I have been a NancyFx user for almost 3 years now. We have a complex set of REST services built on top of Nancy with custom security and cache loading incorporated into the startup and Before pipelines in Nancy. Nancy is by far the best low ceremony web framework I have used!

Thanks for spotlighting this great framework for the .NET community!

Wednesday, 03 August 2016 19:48:37 UTC
Leave it up to a developer to call his library Nancy.

Pesonally I would have went with Natasha. xD
Friday, 05 August 2016 10:27:54 UTC
Hey Scott,

I am user of NancyFx for almost 2 years . We have a complex set of REST services built on top of Nancy with custom security and cache loading incorporated into the startup and Before pipelines in Nancy.
Saturday, 06 August 2016 14:00:23 UTC
Nancy Sinatra haha, awesome :)

As always, thanks Scotty!
Wednesday, 10 August 2016 14:47:55 UTC
Why does OWIN appear here? I thought it is dead now in .NET Core? Can someone please explain it?
Friday, 12 August 2016 08:52:50 UTC
A few web app flavors, test projects, and a very simple Web API application that returns JSON and generally tries to be RESTful.
best ed treatment
Saturday, 13 August 2016 20:05:02 UTC
Great stuff :)
As always, thanks Scott!
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.