This week’s blog is from Adam Driscoll (Blog | Twitter), software engineering manager at Dell, PowerShell MVP, and Tech on Tap – Nothing But .NET speaker. He’s going to introduce us to the Nancy framework.
Nancy is a lightweight framework for building web applications in .NET. It’s easy to get up and running and can be extended to develop complex web apps with minimal overhead. In this post we’ll look at how to get a simple Nancy instance running and serving web pages.
First, create a new console application in Visual Studio. Next, open the Package Manager Console and type ‘Install-Package Nancy’ and then ‘Install-Package Nancy.Hosting.Self’.
We now have Nancy installed with the self-hosting module referenced. This will allow us to stand up our own Nancy instance outside of any other web server like IIS. Next, we need to create a NancyModule. Nancy modules define how the Nancy server will respond to requests.
Nancy modules are automatically discovered. In this module, we handle requests coming into the server at the /beer endpoint. We will return a Beer view that dynamically populates a list of beers.
The Beer.sshtml file is included with the project as Content and copied to the output directory.
Using some simple syntax, we can auto-generate the HTML markup for creating a list item for each one of the beers created in the module.
Finally, we need to create the Nancy self-host when the console application runs. We also need to tell Windows to open the ports and reserve the URL that we are specifying. If this is not done, the server will receive an Access Denied error and we will not be able to navigate to the page.
Now it’s time to test the Nancy module. When run the first time, the Nancy host will prompt for elevation as it executes a couple netsh commands to open the routes to the server. Subsequent runs may not require this elevation. Once the host is up and running we can navigate to the endpoint with any HTML browser or client.
Nancy has much more to it than what was covered in this post. I suggest you run over to the Nancy Wiki for more information about this neat little web framework.
You can talk to Adam and learn more at Tech on Tap – Nothing But .NET on Saturday, February 28, 2015 – sign up today!