Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the latest VB.NET language level in an ASP.NET web site project?

I've been tasked with modernising a web application developed in 2009. It is written in VB.NET and using ASP.NET WebForms. I would like to use the latest language constructs in VB.NET.

I get helpful pointers saying "Visual Basic 10.0 does not allow string interpolation", which was what I was trying to use, but I failed to find a way to raise the language level.

I have the tips in this related question How to change the VB.NET language version in Visual Studio 2015, but they did not help in this context.

In ReSharper properties, I could set the "VB Language Level" for each of the four different projects to "Visual Basic .NET 15". This changes a line of XML in the project's .DotSetting file, and this setting changes how ReSharper analyzes the code, but alas, this did not take away the compilation errors.

How do I enable support in Visual Studio 2017 for the latest version of VB.NET in an ASP.NET Web Site or Web Application project?

like image 917
NilsAndreas Avatar asked Mar 02 '18 14:03

NilsAndreas


People also ask

Can I use VB.NET for web development?

Developing AutoCAD Plugins using VB.NET with Windows Forms ASP.Net is the . Net version of ASP, introduced by Microsoft, for creating dynamic web pages by using server-side scripts. ASP.Net applications are compiled codes written using the extensible and reusable components or objects present in . Net framework.

What language does VB.NET use?

NET languages, VB.NET uses the Common Language Runtime (CLR) for program execution. VB.NET is substantially different from traditional Visual Basic, which has been the most popular language for developing Windows applications. See CLR and Visual Basic.

Is VB.NET and ASP.NET the same?

ASP.NET is for developong web applications (Web pages) once Web application is hosted once should have internet to access that web pages. VB.NET is for developing Windows applications ( like a software which can be installed in your system and you can run it without the help of internet Ex: VLC Player,...

Is VB NET a good language to learn?

VB.Net programming is very much based on BASIC and Visual Basic programming languages, so if you have basic understanding on these programming languages, then it will be a fun for you to learn VB.Net programming language. AutoCAD Programming using VB.NET - Hands On!

How to set the language version of the Visual Basic language?

There are three ways to set the language version: You can set the language version in your .vbproj file. Add the following element: The value latest uses the latest minor version of the Visual Basic language. Valid values are:

What is VB NET?

VB.Net is a simple, modern, object-oriented computer programming language developed by Microsoft to combine the power of .NET Framework and the common language runtime with the productivity benefits that are the hallmark of Visual Basic. This tutorial will teach you basic VB.Net programming and will also take you...

How do I change the language of a Visual Studio project?

Set the language version in Visual Studio. You can set the version in Visual Studio. Right-click on the project node in Solution Explorer and select Properties. Select the Build tab and select the Advanced button. In the dropdown, select the version.


2 Answers

To use the latest VB.NET or C# with ASP.NET Web Applications and Web Sites projects you need to install or update two Rosyln Nuget packages.

Microsoft:

When you have a solution open which has at least one web project which is targetting .NET 4.5+ and does not have the DotNetCompilerPlatform NuGet package in the Project menu you’ll see a new option, Enable C# 6 / VB 14 appear.

The following screenshot is from VS2015 but the option should be present in VS2017 too:

enter image description here

Alternatively, bypass the above GUI feature and just go directly to the Nuget package manager and install the latest version of the following packages into the project/site to be as up-to-date with the VB.NET language as it is possible to be:

  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform
  • Microsoft.Net.Compilers

(NB The latest releases are demanding .NET 4.6+ is installed on the system for the compiler to run, but can compile code targeting any platform).

This will likely get you sorted for C# 6/VB 14. There is one last step to get VB 15: edit the langversion in the web.config file so that it reads 15.0 (or latest if you want to be on the newest version - this in my preferred option).

<system.codedom>
  <compilers>
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:15.0 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
 </compilers>

Unfortunately I have found myself having to edit the langversion after upgrading my Roslyn to C# 7.0 and then to 7.1 so it is definitely something to look out for.

By the way, this answer doesn't just apply to old projects - even new web projects created in VS 2017 need this fix if the template used doesn't reference the Nuget compiler packages. Roslyn is not mandatory and the template for e.g. a new Web Site has reportedly not been updated.

like image 189
Stephen Kennedy Avatar answered Oct 17 '22 13:10

Stephen Kennedy


The solutions was quite easy:

  • Update Nuget packages for solution, install "Microsoft.CodeDom.Providers.DotNetCompilerPlatform". That will also install Microsoft.Net.Compilers
  • Then, upgrade the version of Microsoft.Net.Compilers to the latest stable version
like image 36
NilsAndreas Avatar answered Oct 17 '22 15:10

NilsAndreas