如何在ASP.NET网站项目中使用最新的VB.NET语言级别? [英] How can I use the latest VB.NET language level in an ASP.NET web site project?
问题描述
我的任务是现代化2009年开发的Web应用程序.它是用VB.NET编写并使用ASP.NET WebForms编写的.我想在VB.NET中使用最新的语言构造.
我得到了有用的提示,说"Visual Basic 10.0不允许字符串插值",这是我一直在尝试使用的方法,但是我没有找到提高语言水平的方法.
我在此相关问题中有提示
或者,绕过上面的GUI功能,直接进入Nuget程序包管理器,并将以下程序包的最新版本安装到项目/站点中,以使其与VB.NET语言保持最新.可能是:
(NB最新版本为要求 .NET 4.6+是已安装在系统上供编译器运行,但可以编译针对任何平台的代码.
这可能会使您对C#6/VB 14进行排序.获得VB 15的最后一步是:在web.config文件中编辑langversion,使其读取 15.0
(或 latest
(如果要使用最新版本-这是我的首选).
< system.codedom><编译器>< 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"警告级别="4"编译器选项="/langversion:15.0/nowarn:41008/define:_MYTYPE = \&" Web \& quot;/optionInfer +"/></compilers>
不幸的是,在将Roslyn升级到C#7.0,然后再升级到7.1之后,我发现自己不得不编辑 langversion
,所以绝对值得关注.
顺便说一句,此答案不仅适用于旧项目-如果使用的模板未引用Nuget编译器软件包,即使在VS 2017中创建的新Web项目也需要此修复程序.Roslyn不是强制性的,例如据报道,一个新的网站尚未更新.
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?
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.
这篇关于如何在ASP.NET网站项目中使用最新的VB.NET语言级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!