Introduction

After having tried out Servicestack it is time to try Nancy.

Nancy is a lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono. The goal of the framework is to stay out of the way as much as possible and provide a super-duper-happy-path to all interactions.

Nancy has some nice docs and I like that.

And Nancy has a nice ecosystem and dedicated masters.

Hello world

Hello world should be easy enough. Just create an empty asp.net project and then add the nuget package. And some code.

As you can see Nancy has a buttload of packages on nuget.

We pick the Nancy.Hosting.AspNet for this one.

Interesting to note that there is also a self hosting one. I could have used that one for the easyhttp testing maybe.

That package adds this web.config for you.

XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
 
http://go.microsoft.com/fwlink/?LinkId=169433
 
  -->
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <httpHandlers>
      <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    </handlers>
  </system.webServer>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit

http://go.microsoft.com/fwlink/?LinkId=169433

  -->
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <httpHandlers>
      <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    </handlers>
  </system.webServer>
</configuration>

Now add a Class and inherit it from NancyModule.

vb.net
1
2
3
4
5
6
7
8
9
10
Imports Nancy
 
Public Class HelloModule
    Inherits NancyModule
 
    Public Sub New()
        MyBase.Get("/") = Function(parameters) "Hello World"
    End Sub
 
End Class
Imports Nancy

Public Class HelloModule
    Inherits NancyModule

    Public Sub New()
        MyBase.Get("/") = Function(parameters) "Hello World"
    End Sub

End Class

Run it and see the magic.

In essence you are looking at the root route and send it hello world when that route is requested.

So when I try another route than just http://localhost/ I will get a 404.

Cool.

Now I can just add that route, for instance http://localhost/plant

vb.net
1
2
3
4
5
6
7
8
9
10
11
Imports Nancy
 
Public Class HelloModule
    Inherits NancyModule
 
    Public Sub New()
        MyBase.Get("/") = Function(parameters) "Hello World"
        MyBase.Get("/plant") = Function(parameters) "Hello Plant"
    End Sub
 
End Class
Imports Nancy

Public Class HelloModule
    Inherits NancyModule

    Public Sub New()
        MyBase.Get("/") = Function(parameters) "Hello World"
        MyBase.Get("/plant") = Function(parameters) "Hello Plant"
    End Sub

End Class

you will get this.

Now I know you should put each route into it’s own class, but spaghetti code is so much nicer.

Conclusion

It took me all of five minutes to get started, mainly due to the not being able to copy paste code. But it works as advertised. Maybe in the next post I will try the Self thing for the easyhttp tests.