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: testing your modules.Nancy and VB.Net: getting data in your page »
    comments

    Introduction

    Yesterday I started using Nancy, and I used the SuperSimpleViewEngine but I was told to use the razor view engine instead. And because I'm a good boy I did that.

    Getting it to work

    First thing to do is to install the Nancy.Viewengines.Razor package from nuget. This of course downloads the needed assemblies and adapts your webconfig.

    And it makes a mistake when adapting the webconfig.

    Your webconfig will look like this when it is done.

    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.     <compilation debug="true" targetFramework="4.0">
    15.       <buildProviders>
    16.         <add extension=".cshtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyCSharpRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" />
    17.         <add extension=".vbhtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyVisualBasicRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" />
    18.       </buildProviders>
    19.     </compilation>
    20.   </system.web>
    21.   <system.webServer>
    22.     <validation validateIntegratedModeConfiguration="false" />
    23.     <handlers>
    24.       <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    25.     </handlers>
    26.   </system.webServer>
    27.   <appSettings>
    28.     <add key="webPages:Enabled" value="false" />
    29.   </appSettings>
    30. </configuration>

    The problem being that it adds this line <compilation debug="true" targetFramework="4.0"> while I already have this line <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" /> and that seemed to be undesirable.

    I fixed it by removing the first line and changing the second.

    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.     <httpRuntime targetFramework="4.5" />
    10.     <httpHandlers>
    11.       <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    12.     </httpHandlers>
    13.     <compilation debug="true" strict="false" explicit="true" targetFramework="4.5">
    14.       <buildProviders>
    15.         <add extension=".cshtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyCSharpRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" />
    16.         <add extension=".vbhtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyVisualBasicRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" />
    17.       </buildProviders>
    18.     </compilation>
    19.   </system.web>
    20.   <system.webServer>
    21.     <validation validateIntegratedModeConfiguration="false" />
    22.     <handlers>
    23.       <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    24.     </handlers>
    25.   </system.webServer>
    26.   <appSettings>
    27.     <add key="webPages:Enabled" value="false" />
    28.   </appSettings>
    29. </configuration>

    So now we can move on.

    The Models

    I changed the model to PlantModel because I want to use PlantsModel for my collection of Plants.

    Here is PlantModel.

    1. Namespace Model
    2.     Public Class PlantModel
    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 here is PlantsModel.

    1. Namespace Model
    2.     Public Class PlantsModel
    3.         Public Property Plants As IList(Of PlantModel)
    4.         Public ReadOnly Property NumberOfPlants As Integer
    5.             Get
    6.                 Return Plants.Count
    7.             End Get
    8.         End Property
    9.     End Class
    10. End Namespace

    The views

    And now we can make our view. I could not really find a template on my machine for razor views so I made a html page and changed the extension to vbhtml.

    And this is my view for the PlantModel.

    1. @Inherits Nancy.ViewEngines.Razor.NancyRazorViewBase(Of WebApplication2.Model.PlantModel)
    2.  
    3. <!DOCTYPE html>
    4. <html>
    5. <head>
    6.     <title></title>
    7. </head>
    8. <body>
    9.     <h1>Welcome to the plant page</h1>
    10.     <table>
    11.         <tr>
    12.             <td>Id</td>
    13.             <td>@Model.Id</td>
    14.         </tr>
    15.         <tr>
    16.             <td>Name</td>
    17.             <td>@Model.Name</td>
    18.         </tr>
    19.     </table>
    20. </body>
    21. </html>

    And here is the View for my PlantsModel.

    1. @Inherits Nancy.ViewEngines.Razor.NancyRazorViewBase(Of WebApplication2.Model.PlantsModel)
    2.  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4. <html xmlns="http://www.w3.org/1999/xhtml">
    5. <head>
    6.     <title></title>
    7. </head>
    8. <body>
    9.     <h1>Welcome to the plants page</h1>
    10.     <table>
    11.         <tr><th>Id</th><th>Name</th></tr>
    12.         @For Each plant As WebApplication2.Model.PlantModel In Model.Plants
    13.             @<tr>
    14.                 <td>@plant.Id</td>
    15.                 <td>@plant.Name</td>
    16.             </tr>
    17.         Next
    18.     </table>
    19. </body>
    20. </html>

    Look at the for each I have in there.

    The module

    The next is my module, which I would like to split up in two pieces next time. Because small is better, but for now this works.

    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 {.Plants = New List(Of PlantModel) From {New PlantModel() With {.Id = 2, .Name = "test"}}})
    10.  
    11.                                 End Function
    12.         MyBase.Get("/plants/{Id}") = Function(parameters)
    13.                                          If parameters.id = 1 Then
    14.                                              Return View(New PlantModel() With {.Id = 1, .Name = "test"})
    15.                                          Else
    16.                                              Return View(New PlantModel() With {.Id = 2, .Name = "test"})
    17.                                          End If
    18.                                      End Function
    19.  
    20.     End Sub
    21.  
    22. End Class

    As you can see I have a Plants route which will show you all the plants. And A plants route which accepts an id which I can read and send the data as requested. Not very good at this point in time but you get the drift.

    And these are the results of the Belgian jury.

    When using the url http://localhost/plants I get this.

    When using the url http://localhost/plants/1 I get this.

    And when using http://localhost/plants/abc I get an exception of course.

    And this funny little fellow on my page.

    Conclusion

    Everything seemed to be reasonable, lost a little time with the webconfig thing and looking up syntax for razor, but it was pretty much ok and smooth for the rest.

    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 The parameter is dynamic and has some extension methods off it.

    You can do:

    Dim poo As Int32 = parameter.id.TryParse(123)

    If it can't parse to an int, it will return 123.

    There's also Default(Of Int32)() Default(123) TryParse(Of Int32)()

    Etc.

    https://github.com/NancyFx/Nancy/blob/master/src/Nancy.Tests/Unit/DynamicDictionaryValueFixture.cs#L473
    12/12/12 @ 02:40

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