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 construc
The solutions was quite easy:
And this is the solution to get rid of the red squiggles underneath any language construct that Visual Studio 2017 thinks is unsupported.
The web.config contained this:
<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:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
in which /langversion:default
had to be replaced with /langversion:14.0
like this:
<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:14.0 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
I found a reference to this on https://github.com/aspnet/RoslynCodeDomProvider/issues/16
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:
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:
(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=\"Web\" /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.
This worked for me -- see /langversion:latest
below
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:latest /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:latest /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>