Hello World Application in MVC 4 VB
Account Home | Help | Blog | Contact us | Log Out


Welcome to Kbytes > Articles

Hello World Application in MVC 4 VB

Posted By: siteadmin on 12/09/2013 19:45:00

There are a lot of resources on the internet about MVC 4 with C#,  but a dearth of those about MVC 4 VB.
 
We could not find even a simple Hello World demonstration using MVC 4 and Visual Studio 2012 VS 2012
 
You can download the source files here
 
This demo is a translation into VB of Roger Harford great video "Introduction to ASP.NET MVC Part 2/3: Creating a Hello World Site"
 
http://www.youtube.com/watch?v=rejv7S8SYjw&list=WL46D062071AA44E03
 
In Step 1 of his video he places the Route in the Global.asax file,  whearas in my version the route is added to the \App_Start\RouteConfig.vb
 
This file has the following code:
 
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.Web.Routing
 
Public Class RouteConfig
    Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
 
        routes.MapRoute( _
            name:="Default", _
            url:="{lang}", _
            defaults:=New With {.controller = "HelloWorld", .action = "Index", .lang = UrlParameter.Optional} _
        )
 
      
    End Sub
End Class
 
 
Model:
 
The model is called HomeModel.vb in the \Models folder with the following code:
 
Public Class HomeModel
 
    Public Property message As String
 
End Class
 
The "Model" is a great place to add Class functions that handle data access, and business logic.  The constructor can load in the data from a database, and then various methods can manipulate it.
 
View:
 
There needs to be a folder called \Views\HelloWorld
 
and in there the file:  Index.vbhtml
 
@modeltype HelloWorld.HomeModel 
@Model.message
 
The "View" is ideally only for presentation.  You can add logic, but this should be restricted to logic that effect the presentation.
 
Controller:
 
In there \controllers folder there needs to be a file called HelloWorldController.vb with the code:
 
Imports HelloWorld.HomeModel
 
Namespace HelloWorld
    Public Class HelloWorldController
        Inherits System.Web.Mvc.Controller
 
 
 
        ' GET: /HelloWorld
 
        Function Index(lang As String) As ActionResult
            Dim model As New HomeModel
 
            If lang = "spanish" Then
                model.message = "Hola, Mundo"
            Else
                model.message = "Hello World!!"
            End If
 
            Return View(model)
        End Function
 
    End Class
End Namespace
 
The "Controller" is a good place to call methods and access properties of classes defined in the "Model", in particular those that effect presentation which is sent down to the "View" HTML pages.
 
Your Solutions explorer should look like this:
 
HelloWorld MVC 4 VB
 
 
 
 
 

blog comments powered by Disqus

Kbytes Home | Privacy Policy | Contact us | Testing Area

© 2004 - 2018 1 Oak Hill Grove Surbiton Surrey KT6 6DS Phone: +44(020)82617371