TQ
dev.com

Blog about software development

Subscribe

Web development in Visual Basic .NET 6

24 Aug 2022 - by 'Maurits van der Schee'

In 2015 I was working on a Simple web framework for .NET. Back then I wanted to create an MVC framework that would allow me to run .NET web applications on Linux. Last year I ported the code .NET 5 (cross-platform now) and today I updated the code to support .NET 6. In this post I will show how to write a Visual Basic web application.

Install .NET 6 on Ubuntu 22.04

If you are not on Windows and cannot install Visual Studio Community Edition 2022 then these instructions may be welcome to you. On Ubuntu 22.04 you need to execute:

sudo apt install dotnet6

Now you have the "dotnet" command on your command line and I would recommend that you install Visual Studio Code and the VB.NET extension "VB.NET Grammars and Snippets" and "aspnet-beautify".

How to get started

Would you like to play with this minimalistic MVC framework? First you need to check whether or not you can clone and run the example project:

git clone https://github.com/maussoft/mvc-example-vb
cd mvc-example-vb
dotnet run

Now you have the following directories in your project directory:

Now let me show you how to add some code.

Writing the code

The webserver uses the following session class in Session.vb:

Imports System.Collections.Generic

Public Class Session
    Public Property Items As New List(Of String)
End Class

This Session class can hold (todo) items in a list.

Now let's create a simple controller in Controllers/Todo.vb:

Imports Maussoft.Mvc

Namespace Controllers
    Public Class Todo

        Public Sub Add(context As WebContext(Of Session))
            context.Data.Title = "Add Todo"
            If context.Method = "POST" Then
                context.Data.Values = New ViewData(context.Post)
                context.Data.Errors = New ViewData()
                If context.Data.Values.Item.Length = 0 Then
                    context.Data.Errors.Item = "Field is mandatory"
                End If
                If context.Data.Errors.Empty() Then
                    context.Session.Items.Add(context.Data.Values.Item)
                    context.Redirect("/Todo")
                    Return
                End If
            Else
                context.Data.Values = New ViewData()
            End If
        End Sub

        Public Sub Index(context As WebContext(Of Session))
            context.Data.Title = "List Todos"
        End Sub

    End Class

End Namespace

Then create a Layout in Views/Layouts/Default.aspx:

<%@ Master %>
<!DOCTYPE html>
<html>
    <head>
        <title>
            <%= Context.Data.Title %>
        </title>
    </head>
    <body>
      <% RenderViewContent() %>
    </body>
</html>

And add a view in Views/Todo/Add.aspx:

<%@ Page Inherits="Layouts.Default" %>
<form method="post">
    <div>
        <label>Item</label><br>
        <input type="text" name="Item" value="<%= Context.Data.Values.Item %>" /><br>
        <% If Context.Data.Errors?.Item.Length Then %>
            <span><%= Context.Data.Errors.Item %></span><br>
        <% End If %>
        <input type="submit" value="Add" />
    </div>
</form>

And another view in Views/Todo/Index.aspx:

<%@ Page Inherits="Layouts.Default" %>
<ul>
    <% For Each item in Context.Session.Items %>
        <li><%= item %></li>
    <% Next %>
</ul>
<a href="/Todo/Add">Add</a>

And then run the application using:

dotnet run

And access it on:

http://localhost:2345/Todo

You will see a very simple Todo application with a list stored in the Session.

See: https://www.nuget.org/packages/Maussoft.Mvc

Happy programming!

Links


PS: Liked this article? Please share it on Facebook, Twitter or LinkedIn.