Blog about software development


Visual Basic in ASP.Net Core 2.1

18 Aug 2018 - by 'Maurits van der Schee'

I love programming in Visual Basic. It gives me a feeling of nostalgia. It reminds me of my early programming days in which I was programming GW-Basic, QBasic and Turbo Basic. I was still in primary school and for me computers were full of secrets and had unlimited possibilities. The Basic programming language survived the test of time and became Visual Basic.

VB.NET is no longer a 1st-class citizen

Later Visual Basic was the second language of Microsoft's .Net platform. Unfortunately (for Basic programmers) that second position (behind C#) seems to have been overtaken by F#. I conclude this because some of the new features of the .Net platform (such as the "VB Razor templates" and "Nullable reference types") have not been implemented for VB in ASP.Net Core.

Example Web Application is missing

Nevertheless you can write a web application in ASP.Net Core 2.1, but not so easy. Visual Studio 2017 is missing the Visual Basic Web Application example for ASP.Net Core. This shouldn't hold you back as you can simply port the C# one to VB.net with a little help from the Telerik Code Converter. The next problem you run into is the lack of vbhtml (VB Razor templates) support.

Lack of vbhtml (VB Razor templates) support

I looked into creating my own vbhtml support by writing a Single File Generator in Visual Studio 2017. Support for Razor in VB is quite complicated according to Microsoft. Porting the ASPX View Engine a.k.a. WebFormViewEngine to .Net Core is probably easier. Although it would require to have the missing XSS protection and TDD support features added, as you can see here.

Bug in build process

I accepted that I needed to write my templates in cshtml (Razor C# dialect). Still I ran into an error while compiling:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
    An unhandled exception has occurred while executing the request.
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
wjrfi1bi.4iy(1,1): error CS8301: Invalid name for a preprocessing symbol; ',NETCOREAPP,NETCOREAPP2_1' is not a valid identifier
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CompileAndEmit(RazorCodeDocument codeDocument, String generatedCode)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CompileAndEmit(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.OnCacheMiss(String normalizedPath)
--- End of stack trace from previous location where exception was thrown ---

This is caused by a wrongly created "bin/Debug/netcoreapp2.1/App.deps.json" having the string ",NETCOREAPP,NETCOREAPP2_1" which should be replaced by "NETCOREAPP2_1". After your first build you can change this and then your incremental builds will (probably) not fail. Maybe I'm doing something wrong or maybe this is indeed a bug.

Code: https://github.com/mevdschee/aspnetcorevb

Happy coding!

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