Scott Hanselman

One ASP.NET: Nancy.Templates for Visual Studio

May 07, 2013 Comment on this post [32] Posted in ASP.NET | Open Source
Sponsored By
NancyFX inside Visual Studio

I hope you've updated to Visual Studio 2012.2 and picked up Web Essentials because we're continuing to add goodness all the time. As we march forward with the One ASP.NET vision, so does the community. One of the major goals has been to make it easier for the community to not only make templates but also live alongside ASP.NET templates as peers. This has been historically hard. It's still too complex, in fact, but it's easier than before. I'm hoping that one day soon making templates will be as easy as making and sharing NuGet packages.

Not only is most of ASP.NET open source, but so are the Azure SDKs, NuGet and more. However, there's a large and rich world of open source frameworks and projects that some companies never get to use either because their company isn't into Open Source or because they won't use code that doesn't come from Microsoft.

Additionally, Microsoft, IMHO, has done a poor job (as a collective) letting developers know that there are options and options are good. Personally, I don't care if you use Entity Framework, or Web Forms, or MVC or Web API. You can use NHibernate, Nancy, Simple.Web, ServiceStack and OpenRasta. It makes no difference to me or my organization. If you are happy and using .NET, then I'm happy and that's great. Microsoft want you to use Azure and Windows, I'm sure, but after that ultimately the rest is just the details of your stack. You should explore the options available and work within your organization to be successful.

On the topic of options, NancyFx is an open source web framework for .NET that uses the Ruby Framework "Sinatra" as its inspiration. (Get it? Frank Sinatra's daughter is Nancy Sinatra.)

A few days ago (with some gentle prodding, and some great team effort) the NancyFx team created a VSIX to integrate the NancyFx Web Framework into the Visual Studio project dialog. (You can also get NancyFx from NuGet, of course.)

It's totally simple from your point of view, as it should be. Download the VSIX, and double click on it. Done.

Installing NancyFX into Visual Studio

Now, in Visual Studio, just File | New Project and  you've got more choices!

NancyFX in the Visual Studio File New Project Dialog

Nancy is a very lightweight and flexible framework for web sites and services. You don't even need ASP.NET proper. You can self-host in your own services or exe, or host within ASP.NET. You can use Razor syntax or choose other View Engines.

Here's Hello World:

public class SampleModule : Nancy.NancyModule
public SampleModule()
Get["/"] = _ => "Hello World!";

Interesting and very concise convention, eh? The Get["/"] syntax is a route, saying that an HTTP GET for / should be handled by this => anonymous method. It's all C#, mind you!

Here's the next step, passing a model into a View:

 public class SampleModule : NancyModule
public SampleModule()
Get["/"] = parameters => {
var person = new Person {
Id = 1,
Name = "Scott Hanselman",
Content = "Lorem ipsum...",
Tags = {"c#", "aspnet", "oss", "nancy"}

return View["Index", person];

Nice, eh? Very familiar if you're comfortable with ASP.NET MVC and general MVC-style frameworks. 

If you want to return Json, you can take an object and call AsJson() like this:

Get["/person"] = parameters =>
var person = new
Id = 1,
Name = "Scott Hanselman",
Content = "Lorem ipsum..."
return Response.AsJson(person);

UPDATE: I am reminded by commenters that Nancy now supports Content Negotiation ("conneg") and will automatically return the format that the client requested. This means if your HTTP headers say "accept: application/json" then you'll get JSON back automatically. Lovely, and no need for the AsJson() (unless that's what you wanted).

There's samples you can see at and you are encouraged to add your own sample. Just File | New Project, and make a Nancy Demo Application and follow the instructions.

NancyFx has a great community of users and you'll  often find them chatting on Jabbr or in their Google Group. There's even a Resharper Plugin for Nancy. Nancy also runs nicely on Mac with Mono and MonoDevelop.

Kudos and congrats to the Nancy team for being awesome, and for making these templates.

Related Links

SPONSOR: Big thanks to the feed sponsor this week, Ext.NET (seriously, check out their demos, really amazing stuff!) - Quickly build modern WebForm and MVC (including RAZOR) Apps for ASP.NET. Free pancake breakfast with all purchases!

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
Hosting By
Hosted in an Azure App Service
May 07, 2013 1:50
I'm hoping that one day soon making templates will be as easy as making and sharing NuGet packages.

Me too! Instead of making them "as easy as" NuGet packages, let's actually use NuGet packages themselves for this. While we're at it, let's make it so you can easily mix and match them (ie create a Nancy project using Entity Framework, Autofac, and MSTest), and go back to the "Project creation" dialog and change your choices after you've already created the project.

I proposed this a bit ago on the ASP.NET forms: Flexible Project Templates
May 07, 2013 1:56
Wow! Coooooooooooooool!
May 07, 2013 1:57
I've never heard of Nancy before. Thanks for the heads up! The syntax looks a little unfamiliar to me but it looks powerful.
May 07, 2013 2:15
Awesome, great to see another easy way to help people adopt and explore OSS frameworks. I love Nancy!
May 07, 2013 7:04
Awesome! <3 Scott.
May 07, 2013 9:39
Going to check it out, thanks do sharing.
May 07, 2013 11:40
Nancy has conneg built-in so no need to do AsJson() anymore ;-)
May 07, 2013 12:16
Also peeps - if people have any more questions about NancyFX (what is it? Need help starting? why should I use NancyFx over MVC, etc?) don't forget you can ask questions in the official NancyFX room over at JabbR :-

Cya in there!
May 07, 2013 12:20
@Franc makes a good point :)

Can read this to find out more about conneg with NancyFX.
May 07, 2013 13:02
@Scott this package looks totally awesome!!!
May 07, 2013 18:00
Hey Scott, Can you please give us some real-world scenarios where nancy would be a nice fit?
May 07, 2013 22:49
Franc and Phillip - Thanks! I've updated the post!
May 07, 2013 23:31
@Sandesh, anywhere where you'd use ASP.NET MVC / WebForms
May 08, 2013 0:04
Great stuff! Wondering if in the future Get["/person"] could be replaced with Get.Person via dynamic type.
May 08, 2013 11:03
Great post :),

I really like the idea of having multiple choices for different circumstances, and I do like some of the ideas provided by NancyFx. But what I do not like is the use of literals throughout the code.

The great thing about MVC is, by default each action name corresponds to a path, like public ActionResult Persons() can be called using /controller/Persons.

If you want to change the route, it only has to be in one place. I see some big problems ahead using NancyFX if you want to use other route names or otherwise want to refactor your code. You can't just rename a string or a dynamic property if you use the same property multiple times.

In my opinion, NancyFX is not mature enough to replace MVC
May 08, 2013 12:20
@Nick - The worst thing about MVC for me is routing. The only way to make Routing bearable is to use AttributeRouting (which is being pulled into MVC 5 thankfully).

The default routing forces you into a particular design of routes/naming, and the moment you need to do something different you need to create a custom route definition, which is hidden away, and non-predictable. The default implementation is also not as flexible as Nancy, again requiring AR to solve a lot of those problems.

Also its not really fair to say its not mature because of literals. Theres plenty of production deployments of large applications using Nancy that get along just fine. A well designed API prevents you worring about things like renaming an action.

I don't want to argue, just my opinion in response to your opinion :)
May 08, 2013 13:32

Nancy is mature enough to make complete enterprise level application. It is just how you want to proceed further. Like, if I start working on something and if I process requirement in GET, POST, PUT, DELETE in REST fashion I will select Servicestack or Web api. Personally I go with Servicestack, just personal choice. But if I feel like request and pages then NancyFX. If I am working in team and I have time and resource to boot the project and wanted to working in convention based environment will go for Asp.Net MVC or Fubu MVC. It just a choice nothing else. Even I give equal preference for C# and F#. As normally Scott use to tell, use what you fill using. Its just like that. For further clarification this article may help

@scott Thanks for great article.
May 08, 2013 14:36
Scott I use MVC and Web API please care :)
May 09, 2013 13:55
In what sense that Nancy is lightweight? How about the performance?

I tried creating 2 web applications. The first one is using Nancy and the second one is an empty MVC4 application. In empty MVC4 applications I added Home Controller and Index View which contains "Index" text only. On Nancy web application the Index view contains also "Index" text only. Running the two applications I observed the rendered time using Firebug. I found out that Nancy Index page is loaded as fast as 14ms while MVC4 Index is loaded as fast as 4ms. I keep on trying thinking that it happened only by chance but no luck. MVC4 is loading faster than Nancy.

My conclusion is Nancy CANNOT offer better performance compare to MVC Framewowrk base on my bench mark.
May 09, 2013 20:08
@Jobert - Run the test in Release Mode and its identical/better than MVC. Also benchmarks with WebAPI/ServiceStack/Nancy, puts Nancy 2x faster than WebAPI while ServiceStack is about 3x faster than WebAPI.

Your firebug test is meaningless.

Also NancyFX has 0 dependency on System.Web, so yes its very lightweight.
May 09, 2013 20:14
For those new to Nancy checkout this blog post - Why use Nancy?
May 10, 2013 4:00
Awesome! I never imagined that I will see Microsoft participate in anything open source. How have the times changed and its a good thing.

I also recently also found out about Django support in Visual Studio 2012.

If you are interested in more Microsoft-Open-Source goodness then checkout
May 10, 2013 10:25
This is one of the two frameworks I would like to use for lightweight web projects. The another is node.js
May 11, 2013 7:10
When MVC was introduced in 2009, we embraced to this framework thinking it's the best web technology exists so far. Now due to the efforts of other developers better options are unveiling. Thanks for sharing this guys!
May 13, 2013 8:44
very nice
May 20, 2013 6:46
I'm probably missing it but I don't see a link to the VSIX in the article. Installing Nancy from NuGet does not add the templates.

I've googled and looked around the Nancy web site but don't see the VS addin.

Thanks, Scott
May 20, 2013 6:49
Found them on github:
May 20, 2013 6:59
Hmmm, I take that back. I think it might be these:
May 20, 2013 7:05
Uhhggg. I don't use add-ins very much so I forgot about the extensions and updates. It's also here:
June 21, 2013 14:16
July 20, 2013 14:43
I never used NancyFx, it's great
September 18, 2013 20:21
Great information. Thanks for sharing!

Comments are closed.

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