VB.NET WinForms and ASP.NET Core API

Neil HaddleyMarch 27, 2023

VB.NET WinForms Development

How do I create a VB.NET Forms app connected to an ASP.NET Core API?

To create a VB.NET Forms app connected to an ASP.NET Core API, you can follow these general steps:

Create an ASP.NET Core API project:

You can create an ASP.NET Core Web API project using Visual Studio 2019 or newer. Choose the ASP.NET Core Web Application template and select the API project type.

Define your API endpoints:

Define your endpoints in the API project, and make sure they return the data your VB.NET Forms app will use. You can use various methods to create endpoints, such as MVC controllers or routing.

Build and run the API project:

Build and run your API project to ensure it's working correctly. You can use tools like Postman or curl to test the endpoints and verify that they return the expected data.

Create a VB.NET Forms app:

Create a new VB.NET Forms app project in Visual Studio. Add any necessary controls to the form, such as text boxes or buttons, that will interact with the API.

Add a reference to the API project:

Add a reference to the API project in your VB.NET Forms app project. You can do this by right-clicking on the project in the Solution Explorer and selecting "Add Reference."

Make API calls from your VB.NET Forms app:

Use the HttpClient class in your VB.NET Forms app to make requests to the API. You can use methods such as GetAsync or PostAsync to retrieve or send data to the API.

Process the API responses in your VB.NET Forms app:

Handle the responses from the API in your VB.NET Forms app. You can deserialize JSON responses into objects using libraries such as Newtonsoft.Json. You can then use these objects to populate the controls on your form.

These are just general steps, and the specific implementation may vary depending on your requirements.

I created a new project/solution

I created a new project/solution

I selected the ASP.NET Core Web API template (with swagger)

I selected the ASP.NET Core Web API template (with swagger)

I named the Web Application

I named the Web Application

I selected the .NET Core Framework version

I selected the .NET Core Framework version

I tested the API

I tested the API

Using Swagger User Interface

Using Swagger User Interface

I added a second project

I added a second project

I selected the VB.NET Windows Forms App template

I selected the VB.NET Windows Forms App template

I gave the WinForms app a name

I gave the WinForms app a name

I selected a .NET Core version

I selected a .NET Core version

I added a reference from the VB.NET WinForms app to the ASP.NET app (providing the VB.NET application with a understanding of the WeatherForecast type)

I added a reference from the VB.NET WinForms app to the ASP.NET app (providing the VB.NET application with a understanding of the WeatherForecast type)

I selected the API project

I selected the API project

I started the API project (dotnet run)

I started the API project (dotnet run)

I added a button and two labels to the VB.NET WinForm screen and VB.NET code to call the API.

I added a button and two labels to the VB.NET WinForm screen and VB.NET code to call the API.

Form1.vb

TEXT
1Imports System.Net.Http
2Imports WebApplicationAPI
3Imports Newtonsoft.Json
4
5Public Class Form1
6    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
7    End Sub
8
9    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
10        ' Create an instance of the HttpClient class
11        Dim client As New HttpClient()
12
13        ' Set the base address of the API
14        client.BaseAddress = New Uri("https://localhost:7018/")
15
16        ' Send an HTTP GET request to the API and receive the response
17        Dim response As HttpResponseMessage = client.GetAsync("WeatherForecast").Result
18
19        ' Check if the response was successful
20        If response.IsSuccessStatusCode Then
21            ' Read the response content as a string
22            Dim result As String = response.Content.ReadAsStringAsync().Result
23
24            ' Deserialize the JSON response into a .NET object
25            Dim forecast As List(Of WeatherForecast) = JsonConvert.DeserializeObject(Of List(Of WeatherForecast))(result)
26
27            ' Do something with the forecast data
28
29            Label1.Text = forecast.First.Date
30
31            Label2.Text = forecast.First.Summary
32
33        Else
34            ' Handle the error
35        End If
36    End Sub
37End Class