ASP.NET Core 2 will come with full support for VB.NET. In current preview we can use VB.NET to build .NET Core applications. I was still successful on mixing up C# and VB.NET project files and port C# code over to VB.NET and build proof-of-concept level web API applicaton that returns fake data about current weather in different cities.
NB! VB.NET support in ASP.NET Core 2 is still under construction and not all things work like expected. This blog post is not a signal to start moving all your current VB.NET web projects to ASP.NET Core. Still it is possible to start playing with early bits published by Microsoft and find out how things work with next version of ASP.NET Core.
NB! Current Visual Studio 2017 Preview is the most convenient option to get started with VB.NET on .NET Core.
I created new VB.NET application for .NET Core and new ASP.NET Core application. After this I took some contents from VB.NET project file to ASP.NET Core application project file and replaced .cs files with .vb ones. I removed everything related to UI as there are things that doesn’t work yet with current preview. But web based API-s usually doesn’t need any UI and we can build these on VB.NET already.
Most of this post is about replacing some C# code with VB.NET one and renaming files. What is “new” stuff is Home controller and model that supports it.
Building weather API application
Let’s start first with Program “class”. In VB.NET we have to define a new module and add Main procedure there.
Module Program
Sub Main(args As String())
Dim builder As IWebHostBuilder
Dim host As IWebHost
builder = WebHost.CreateDefaultBuilder(args)
builder.UseStartup(Of Startup)
host = builder.Build()
host.Run
End Sub
End Module
We also have to modify Startup class of application.
Public Class Startup
Public Sub New(conf As IConfiguration)
Configuration = conf
End Sub
Property Configuration As IConfiguration
Public Sub ConfigureServices(services As IServiceCollection)
services.AddMvc
End Sub
Public Sub Configure(app As IApplicationBuilder, env As IHostingEnvironment)
If env.IsDevelopment Then
app.UseDeveloperExceptionPage
Else
app.UseExceptionHandler("/Home/Error")
End If
app.UseStaticFiles
app.UseMvcWithDefaultRoute
End Sub
End Class
As we want to show data about weather but we don’t want to mess with database and data layer in this sample application we add model class for current weather and make the model class also to hold a test data.
Imports System.Collections.Generic
Namespace Models
Public Class WeatherNowModel
Public Property City As String
Public Property Temperature As Double
Public Shared ReadOnly Property Data As List(Of WeatherNowModel)
Get
Dim testData As List(Of WeatherNowModel)
testData = New List(Of WeatherNowModel)
testData.Add(New WeatherNowModel With {.City = "Tallinn", .Temperature = 5.5})
testData.Add(New WeatherNowModel With {.City = "Helsinki", .Temperature = 3.5})
testData.Add(New WeatherNowModel With {.City = "Odessa", .Temperature = 18})
testData.Add(New WeatherNowModel With {.City = "Varna", .Temperature = 19})
Return testData
End Get
End Property
End Class
End Namespace
Now we have everything we need for simple API and it’s time to write primitive API controller.
Imports System.Linq
Imports Microsoft.AspNetCore.Mvc
Imports WebApplication1.Models
Namespace Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As JsonResult
Dim result = New With {.Product = "Weather API", .Version = "1.0"}
Return Json(result)
End Function
Public Function Now(id As String) As WeatherNowModel
Dim result As WeatherNowModel
result = WeatherNowModel.Data _
.FirstOrDefault(Function(w) w.City.ToLower() = If(id, "").ToLower())
Return result
End Function
End Class
End Namespace
When running application we get the following results.
URL | Result |
/ | {"product":"Weather API","version":"1.0"} |
/home/now/helsinki | {"city":"Helsinki","temperature":3.5} |
Wrapping up
Although tooling for VB.NET projects is not ready yet on ASP.NET Core 2 Preview 1 and VB.NET is not yet fully supported for web projects we can still write something already that doesn’t need user interface. After some dark chemistry with project files we got VB.NET web application that returns fake temperatures in cities. All our code is based on VB.NET and it builds and runs well with current Visual Studio Preview.
View Comments (2)
i need the core of API Web Configuration
Please be more specific - what do you exactly need or where did you got stuck?