Scott Hanselman

One ASP.NET: Nancy.Templates for Visual Studio

May 6, '13 Comments [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 http://samples.nancyfx.org 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. I am 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 ORCS Web
Monday, May 06, 2013 9:50:26 PM UTC
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
Daniel Plaisted
Monday, May 06, 2013 9:56:42 PM UTC
Wow! Coooooooooooooool!
Gyuwon
Monday, May 06, 2013 9:57:14 PM UTC
I've never heard of Nancy before. Thanks for the heads up! The syntax looks a little unfamiliar to me but it looks powerful.
Monday, May 06, 2013 10:15:27 PM UTC
Awesome, great to see another easy way to help people adopt and explore OSS frameworks. I love Nancy!
matt
Tuesday, May 07, 2013 3:04:36 AM UTC
Awesome! <3 Scott.
Tuesday, May 07, 2013 5:39:11 AM UTC
Going to check it out, thanks do sharing.
Tuesday, May 07, 2013 7:40:08 AM UTC
Nancy has conneg built-in so no need to do AsJson() anymore ;-)
Tuesday, May 07, 2013 8:16:54 AM UTC
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 :- https://jabbr.net/#/rooms/nancyfx

Cya in there!
Pure Krome
Tuesday, May 07, 2013 8:20:40 AM UTC
@Franc makes a good point :)

http://www.philliphaydon.com/2013/04/nancyfx-revisiting-content-negotiation-and-apis-part-1

Can read this to find out more about conneg with NancyFX.
Tuesday, May 07, 2013 9:02:05 AM UTC
@Scott this package looks totally awesome!!!
Tuesday, May 07, 2013 2:00:46 PM UTC
Hey Scott, Can you please give us some real-world scenarios where nancy would be a nice fit?
Sandesh
Tuesday, May 07, 2013 6:49:32 PM UTC
Franc and Phillip - Thanks! I've updated the post!
Tuesday, May 07, 2013 7:31:52 PM UTC
@Sandesh, anywhere where you'd use ASP.NET MVC / WebForms
Tuesday, May 07, 2013 8:04:37 PM UTC
Great stuff! Wondering if in the future Get["/person"] could be replaced with Get.Person via dynamic type.
Alex
Wednesday, May 08, 2013 7:03:04 AM UTC
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
Wednesday, May 08, 2013 8:20:05 AM UTC
@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 :)
Wednesday, May 08, 2013 9:32:22 AM UTC
@Nick

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 http://odetocode.com/blogs/scott/archive/2013/02/12/you-want-to-build-web-software-with-c.aspx

@scott Thanks for great article.
Wednesday, May 08, 2013 10:36:11 AM UTC
Scott I use MVC and Web API please care :)
Mike
Thursday, May 09, 2013 9:55:19 AM UTC
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.
Jobert Enamno
Thursday, May 09, 2013 4:08:39 PM UTC
@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.

http://i.imgur.com/n0obYyS.png

Also NancyFX has 0 dependency on System.Web, so yes its very lightweight.
Thursday, May 09, 2013 4:14:50 PM UTC
For those new to Nancy checkout this blog post - Why use Nancy?
Friday, May 10, 2013 12:00:53 AM UTC
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 http://www.microsoft.com/en-us/openness/default.aspx#projects
Friday, May 10, 2013 6:25:03 AM UTC
This is one of the two frameworks I would like to use for lightweight web projects. The another is node.js
Saturday, May 11, 2013 3:10:02 AM UTC
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!
Jobert Enamno
Monday, May 13, 2013 4:44:05 AM UTC
very nice
sanjay mishra
Monday, May 20, 2013 2:46:12 AM UTC
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
Scott
Monday, May 20, 2013 2:49:38 AM UTC
Found them on github: https://github.com/NancyFx/Accessories/blob/master/templates/visual%20studio/projects/Nancy.Empty.Web.Application.Template.Package.vsix
Scott
Monday, May 20, 2013 2:59:13 AM UTC
Hmmm, I take that back. I think it might be these:
https://github.com/NancyFx/Nancy.Templates
Scott
Monday, May 20, 2013 3:05:43 AM UTC
Uhhggg. I don't use add-ins very much so I forgot about the extensions and updates. It's also here: http://visualstudiogallery.msdn.microsoft.com/f1e29f61-4dff-4b1e-a14b-6bd0d307611a
Scott
Friday, June 21, 2013 10:16:12 AM UTC
Coool
Helix
Saturday, July 20, 2013 10:43:07 AM UTC
I never used NancyFx, it's great
Wednesday, September 18, 2013 4:21:10 PM UTC
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.