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: the login/logout thingNancy and VB.Net: Forms authentication »
    comments

    Introduction

    Up until now we have used the browser to look at our data. In other words Nancy created a view for us and returned html to the client that requested that information. But wouldn't it be nice if we could use the same for returning html and/or other formats like json or xml. It turns out to be very easy to do with Nancy.

    The code can still be found on github.

    Content negotiation

    This can only work if Nancy is able to change the response based on the request. In other words content negotiation.

    According to their wiki.

    The content negotiation pipeline will inspect the incoming Accept headers and determine which of the requested media types is the most suitable and format the response accordingly.

    The server

    For this to work we need to change our Module just a little bit.

    Lets take the treesmodule. And change the Get that returns all trees from this

    1. MyBase.Get("/trees") = Function(parameters)
    2.                                        Return View(New TreesModel() With {.Trees = treeService.AllTrees()})
    3.                                    End Function

    to this

    1. MyBase.Get("/trees") = Function(parameters)
    2.                                        Return Negotiate.WithModel(New TreesModel() With {.Trees = treeService.AllTrees()})
    3.                                    End Function

    Instead of returning a View I now use Negotiate.WithModel and that's all the changes I made.

    Easyhttp client

    YOu can now make a newproject and add easyhttp to that and read the list of trees like this.

    1. Imports System.Net
    2. Imports EasyHttp.Http
    3.  
    4. Module Module1
    5.  
    6.     Sub Main()
    7.         Dim http = New HttpClient()
    8.         http.Request.Accept = HttpContentTypes.ApplicationJson
    9.         Dim trees = http.Get("http://localhost:55360/trees")
    10.         For Each t In trees.DynamicBody.Trees
    11.             Console.WriteLine(t.Id)
    12.             Console.WriteLine(t.Genus)
    13.         Next
    14.         Console.ReadLine()
    15.     End Sub
    16.  
    17. End Module

    With this as the result.

    1
    Fagus
    2
    Quercus
    3
    Betula

    And of course all this time the browser client will just keep on working like nothing happened.

    Parameters

    We can now also change our second Get in that module to return the data based on the id. To this.

    1. MyBase.Get("/trees/{Id}") = Function(parameters)
    2.                                             Dim result As Integer
    3.                                             Dim isInteger = Integer.TryParse(parameters.id, result)
    4.                                             Dim tree = treeService.FindById(result)
    5.                                             If isInteger AndAlso tree IsNot Nothing Then
    6.                                                 Return Negotiate.WithModel(tree)
    7.                                             Else
    8.                                                 Return HttpStatusCode.NotFound
    9.                                             End If
    10.                                         End Function

    Again just change the return view call to return Negotiate.WithModel.

    Simples.

    We can now add an easyhttp call like this.

    1. Dim result = http.Get("http://localhost:55360/trees", New With {.Id = 1})
    2.         Dim tree = result.DynamicBody
    3.         Console.WriteLine(tree.Id)
    4.         Console.WriteLine(tree.Genus)

    Sadly that won't work. Because easyhttp translates that call to http://localhost/trees?id=1 and Nancy wants you to use routes instead. Like this http://localhost/trees/1 . I know how easyhttp does this too, since it was me who added that feature to easyhttp ;-). So I guess I will have to change easyhttp to use it this way.

    Anyway if you change your easyhttp call to this

    1. Dim result = http.Get("http://localhost:55360/trees/1")
    2.         Dim tree = result.DynamicBody
    3.         Console.WriteLine(tree.Id)
    4.         Console.WriteLine(tree.Genus)

    it will work, until I have time to harass Hadi enough so that he does the change or (more likely) until I do the change myself.

    Conclusion

    It is extremely easy to make Nancy return the response formatted based on the request.

    About the Author

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

    No feedback yet

    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.)