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.

Namespace Model
    Public Class PlantsModel
        Public Property Id As Integer
        Public Property Name As String
        Public Property LatinName As String
    End Class
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.

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

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

Public Class PlantsModule
    Inherits NancyModule

    Public Sub New()
        MyBase.Get("/plants") = Function(parameters)
                                    Return View(New PlantsModel() With {.Id = 2, .Name = "test"})
                                End Function

    End Sub

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.

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

Public Class PlantsModule
    Inherits NancyModule

    Public Sub New()
        MyBase.Get("/plants") = Function(parameters)
                                    Return View("Plants.html", New PlantsModel() With {.Id = 2, .Name = "test"})
                                End Function

    End Sub

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.