Sponsorship is available! If you’d like to sponsor CodeOpinion.com and have your product or service advertised exclusively (no AdSense) on every post, contact me.Working in the .NET ecosystem can feel very insular. Especially if you work in the enterprise. At the very beginning of my career in software development (circa late ’90s), I was generally using more open tooling and languages. The atmosphere in those communities had (past tense) a much different feeling than it does in the .NET community. Most developers that are in .NET can feel the changes that have been occurring recently. There has been a bigger wave of alternative and open source software awareness within .NET. However, I still don’t think that the .NET community at large are even aware of some of the great open source libraries and frameworks.
ASP.NET MVC / Web ApiIf you were starting new web project in .NET, I think it’s safe to say that most would automatically choose ASP.NET MVC or Web Api. There may be legitimate reasons for doing so, such as you or your team having exiting knowledge of those frameworks. However, NancyFX is a great alternative that is very mature, simple and has great documentation.
NancyFXNancy is a lightweight web framework. It’s really that simple. At its heart it provides a really easy way to create HTTP services. That means you could be generating and returning HTML using the MVC pattern or creating services returning JSON. You are not forced into either or into any heavy configuration. You are simply creating endpoints using HTTP methods.
Getting StartedIn my code examples, I’m going to be using my Self Host ASP.NET Demo application, which is currently demos how to use Katana (Owin) Self Host, Middleware, File Server and Topshelf. I’m going to extend this project to include Nancy. First let’s install Nancy from Nuget
Install-Package NancyFor my example, I’m also going to need to Install the Nancy.Owin Package because my self host application uses the Katanta Owin Implementation with HttpListener.
Install-Package Nancy.OwinNow that we have our packages installed, in your Startup class, use the new IAppBuilder.UseNancy() extension method.
Simplest ExampleMy intent for this blog post is simply to get people looking at Nancy as an alternative. I can’t show you all all the wonderful features in one easy to digest post. In posts to come I’m going to cover many different topics related to Nancy. However, for this post I want to provide the simplest possible example of creating an HTTP endpoint using the GET method. Similar to ASP.NET MVC or Web Api, you will create a class that extends another. For Nancy, we create a class that extends the NancyModule. It is in this class were we define the routes and the endpoint implementation. In my example above, I’ve created a HTTP GET route to /nancy/demo. Since my self host application is running on localhost:8080, this translates to http://localhost:8080/nancy/demo. Here are the results: As you might expect, Nancy has automatically converted my .NET string array to JSON without any configuration.
FeaturesAs I mentioned, I plan on going over in detail many features of Nancy and why you may want to use it. But to get you interested a bit more, here are a few key points.
- Model Binding
- Built in IoC Container (Support for almost all others)
- Easy Testing
- Content Negotiation
- View Engines (including Razor Support)
- Multiple Hosting Options (Katana/Owin, ASP.NET HttpHandler, more)
- Mono Support