Web Development with VB

43.1 Legacy: ASP.NET Web Forms and MVC (.NET Framework)

Web Forms (.aspx + .aspx.vb code-behind): event-driven, server controls (GridView, Button), ViewState, Master Pages. VB was popular here. Still maintained on .NET Framework 4.8 but not available on modern .NET.

ASP.NET MVC 5 / Web API 2 (.NET Framework) support VB controllers and models (with VBHTML Razor views).

43.2 Modern: ASP.NET Core Web APIs in VB

ASP.NET Core has no official VB templates and no VB Razor support (Razor Pages, Blazor, and MVC views require C#). VB can still host controller-based Web APIs and minimal APIs:

Recommended architecture: keep business logic and data access in VB class libraries and build the web front end in C# (Razor/Blazor) or JavaScript. This works seamlessly.

' Program.vb (ASP.NET Core Web API in VB): create a VB console project and add the Microsoft.AspNetCore.App framework reference
Imports Microsoft.AspNetCore.Builder
Imports Microsoft.AspNetCore.Http
Imports Microsoft.Extensions.DependencyInjection
Imports Microsoft.Extensions.Hosting

Module Program
    Sub Main(args As String())
        Dim builder = WebApplication.CreateBuilder(args)
        builder.Services.AddControllers()
        Dim app = builder.Build()

        app.MapGet("/", Function() "Hello from VB.NET on ASP.NET Core!")
        app.MapGet("/api/products/{id:int}", Function(id As Integer) Results.Ok(New With {.Id = id, .Name = "Laptop"}))
        app.MapControllers()
        app.Run()
    End Sub
End Module

<ApiController>
<Route("api/[controller]")>
Public Class CustomersController
    Inherits Microsoft.AspNetCore.Mvc.ControllerBase

    <HttpGet>
    Public Function GetAll() As IEnumerable(Of String)
        Return {"Ana", "Bo"}
    End Function

    <HttpPost>
    Public Function Create(customer As Customer) As Microsoft.AspNetCore.Mvc.IActionResult
        Return CreatedAtAction(NameOf(GetAll), customer)
    End Function
End Class
<!-- .vbproj -->
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>
</Project>