Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Nancy and VB.Net: getting data in your pageSignalR, Quartz.Net and ASP.Net: part 2 the webclient »
    comments

    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.

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

    Now add a Class and inherit it from NancyModule.

    1. Imports Nancy
    2.  
    3. Public Class HelloModule
    4.     Inherits NancyModule
    5.  
    6.     Public Sub New()
    7.         MyBase.Get("/") = Function(parameters) "Hello World"
    8.     End Sub
    9.  
    10. 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

    1. Imports Nancy
    2.  
    3. Public Class HelloModule
    4.     Inherits NancyModule
    5.  
    6.     Public Sub New()
    7.         MyBase.Get("/") = Function(parameters) "Hello World"
    8.         MyBase.Get("/plant") = Function(parameters) "Hello Plant"
    9.     End Sub
    10.  
    11. 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.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    nancy, vb.net
    InstapaperVote on HN

    1 comment

    Comment from: Phillip Haydon [Visitor] · http://www.philliphaydon.com
    Phillip Haydon Finally you got around to Nancy!!!

    You don't need to put each Route* in it's own class. You can put the routes in related modules.

    i.e an AccountModule, ProductsModule, etc. Then in there define all routes related to that module.

    Products may have a /products/{id} and /products, which does listing and view.
    12/11/12 @ 08:16

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)