Sitemap

Step-by-Step ‘Hello World’ API in VB.NET Using Clean Architecture Principles

3 min readSep 6, 2024

--

This is a simple REST API built using VB.NET and ASP.NET Core, following clean architecture principles by separating routing, controllers, and business logic.

Prerequisites

- [.NET SDK](https://dotnet.microsoft.com/download) (latest version)
- A text editor (e.g., [VSCode](https://code.visualstudio.com/))

Project Structure

MyRESTAPIVb/
│
├── Controllers/
│ └── HelloWorldController.vb # Handles HTTP requests
│
├── Services/
│ └── HelloWorldService.vb # Contains business logic
│
├── Models/
│ └── MessageModel.vb # Represents data models
│
├── Startup.vb # Configures services and middleware
├── Program.vb # Entry point for the application
├── MyRESTAPIVb.vbproj # Project file

Project Setup

Step 1: Create a New Console Project

Start by creating a new console project with the VB.NET language option:

dotnet new console -n MyRESTAPIVb -lang VB

This will create a new console application in VB.NET named `MyRESTAPIVb`.

Step 2: Add ASP.NET Core Dependencies

Add the necessary ASP.NET Core packages to convert the console application into a web API:

dotnet add package Microsoft.AspNetCore.Mvc

Step 3: Modify the Project File

Ensure your `MyRESTAPIVb.vbproj` file looks like this:

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
</Project>

This configures the project to use the `Microsoft.NET.Sdk.Web` SDK for ASP.NET Core applications.

Step 4: Create Domain Models

Create a `Models` folder and add a `MessageModel.vb` file:

Namespace Models
Public Class MessageModel
Public Property Message As String
End Class
End Namespace

Get Arif Rahman’s stories in your inbox

Join Medium for free to get updates from this writer.

Step 5: Implement the Business Logic (Service Layer)

Create a `Services` folder and add a `HelloWorldService.vb` file:

Imports MyRESTAPIVb.Models

Namespace Services
Public Class HelloWorldService
' Business logic to get the message
Public Function GetMessage() As MessageModel
Dim response As New MessageModel With {
.Message = "Hello from the service layer!"
}
Return response
End Function
End Class
End Namespace

Step 6: Create the Controller

Create a `Controllers` folder and add a `HelloWorldController.vb` file:

Imports Microsoft.AspNetCore.Mvc
Imports MyRESTAPIVb.Services
Imports MyRESTAPIVb.Models

<ApiController>
<Route("api/[controller]")>
Public Class HelloWorldController
Inherits ControllerBase

Private ReadOnly _helloWorldService As HelloWorldService

' Constructor injection of the service layer
Public Sub New(helloWorldService As HelloWorldService)
_helloWorldService = helloWorldService
End Sub

' Handle GET requests
<HttpGet>
Public Function GetMessage() As ActionResult(Of MessageModel)
Dim message = _helloWorldService.GetMessage()
Return Ok(message)
End Function
End Class

Step 7: Configure Dependency Injection in `Startup.vb`

Update the `Startup.vb` file to register the service layer using ASP.NET Core’s dependency injection system:

Imports Microsoft.AspNetCore.Builder
Imports Microsoft.AspNetCore.Hosting
Imports Microsoft.Extensions.DependencyInjection
Imports Microsoft.Extensions.Hosting
Imports MyRESTAPIVb.Services

Public Class Startup
Public Sub ConfigureServices(services As IServiceCollection)
' Register HelloWorldService for dependency injection
services.AddScoped(Of HelloWorldService)()

' Add controllers
services.AddControllers()
End Sub

Public Sub Configure(app As IApplicationBuilder, env As IWebHostEnvironment)
If env.IsDevelopment() Then
app.UseDeveloperExceptionPage()
End If

app.UseRouting()

app.UseEndpoints(Sub(endpoints)
endpoints.MapControllers()
End Sub)
End Sub
End Class

Step 8: Modify `Program.vb`

The `Program.vb` file configures the application to use the `Startup.vb` file for configuring services and middleware:

Imports Microsoft.AspNetCore.Hosting
Imports Microsoft.Extensions.Hosting

Module Program
Sub Main(args As String())
CreateHostBuilder(args).Build().Run()
End Sub

Function CreateHostBuilder(args As String()) As IHostBuilder
Return Host.CreateDefaultBuilder(args).
ConfigureWebHostDefaults(Sub(webBuilder)
webBuilder.UseStartup(Of Startup)()
End Sub)
End Function
End Module

Step 9: Run the Application

Run the application with:

dotnet run

The API will be available at `http://localhost:5000/api/helloworld`.

Step 10: Test the API

Use `curl`, Postman, or a browser to test the API:

curl http://localhost:5000/api/helloworld

Expected JSON Response:

{
"message": "Hello from the service layer!"
}

Conclusion

This project demonstrates a basic clean architecture for a VB.NET REST API, separating concerns into controllers, services, and models. The architecture is scalable, maintainable, and easy to extend for more complex business logic and features.

--

--