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 razor view engineNancy and VB.Net »
    comments

    Introduction

    A few hours ago I started a website in Nancy. Now I want to show data I have in my webpage.

    And here the docs were not all that helpfull. But I made it.

    The Model

    First of all I need a Model.

    1. Namespace Model
    2.     Public Class PlantsModel
    3.         Public Property Id As Integer
    4.         Public Property Name As String
    5.         Public Property LatinName As String
    6.     End Class
    7. End Namespace

    And I put that in a Model folder. I ended with Model since that is the convention but not really needed in all cases.

    The View

    Time to move on to more exiting things.

    I created a Plants.html file.

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4.     <title>NancyFX</title>
    5. </head>
    6. <body>
    7.     <h1>Plants</h1>
    8.     <div id="plants">
    9.         <p>@Model.Id</p>
    10.         <p>@Model.Name</p>
    11.     </div>
    12. </body>
    13. </html>

    I used the SuperSimpleViewEngine for this.

    As you see I just add the @Model. and then the name of the property I want. Simples.

    Now I just need to tell my controller what to do.

    The Controller

    I guess we can call our module our controller.

    1. Imports WebApplication2.Model
    2. Imports Nancy
    3.  
    4. Public Class PlantsModule
    5.     Inherits NancyModule
    6.  
    7.     Public Sub New()
    8.         MyBase.Get("/plants") = Function(parameters)
    9.                                     Return View(New PlantsModel() With {.Id = 2, .Name = "test"})
    10.                                 End Function
    11.  
    12.     End Sub
    13.  
    14. End Class

    So the url http://localhost/plants will take you to the page Plants.htmland will inject the PlantsModel I just created in there. By convention the fact that you inject a PlanstModel in there informs Nancy that you want to use the Plant.html view.

    You could have written this instead.

    1. Imports WebApplication2.Model
    2. Imports Nancy
    3.  
    4. Public Class PlantsModule
    5.     Inherits NancyModule
    6.  
    7.     Public Sub New()
    8.         MyBase.Get("/plants") = Function(parameters)
    9.                                     Return View("Plants.html", New PlantsModel() With {.Id = 2, .Name = "test"})
    10.                                 End Function
    11.  
    12.     End Sub
    13.  
    14. End Class

    Or any other html.

    And that gives us this amazing page.

    Conclusion

    Slightly harder than it should have been because I could not find the correct docs that explained this simply. But I got it working anyway.

    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 I think most Nancy users are using the Razor view engine so little effort has gone into explaining the SuperSimpleViewEngine.

    Good to know though so we can make a better effort to flesh them out.
    12/11/12 @ 08:23

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