简体   繁体   English

如何在ASP.NET网站项目中使用最新的VB.NET语言级别?

[英]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. 我的任务是使2009年开发的Web应用程序现代化。它是用VB.NET编写的,并使用ASP.NET WebForms。 I would like to use the latest language constructs in VB.NET. 我想在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. 我得到了有用的指针说“Visual Basic 10.0不允许字符串插值”,这是我试图使用的,但我没有找到提高语言级别的方法。

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. 我有相关问题的提示如何在Visual Studio 2015中更改VB.NET语言版本 ,但它们在此上下文中没有帮助。

In ReSharper properties, I could set the "VB Language Level" for each of the four different projects to "Visual Basic .NET 15". 在ReSharper属性中,我可以将四个不同项目中的每个项目的“VB语言级别”设置为“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. 这改变了项目的.DotSetting文件中的一行XML,这个设置改变了ReSharper分析代码的方式,但是,这并没有消除编译错误。

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? 如何在Visual Studio 2017中为ASP.NET网站或Web应用程序项目中的最新版本的VB.NET启用支持?

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. 要将最新的VB.NET或C#与ASP.NET Web应用程序和Web站点项目一起使用,您需要安装或更新两个Rosyln Nuget软件包。

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. 如果您打开一个解决方案,其中至少有一个目标为.NET 4.5+的Web项目,并且项目菜单中没有DotNetCompilerPlatform NuGet包,您将看到一个新选项,即出现启用C#6 / VB 14。

The following screenshot is from VS2015 but the option should be present in VS2017 too : 以下屏幕截图来自VS2015,但该选项也应出现在VS2017中

在此输入图像描述

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: 或者,绕过上面的GUI功能,直接进入Nuget包管理器并将以下包的最新版本安装到项目/站点中,以便与VB.NET语言一致,因为它可能是最新的:

(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). (NB最新版本要求 .NET 4.6+ 安装在系统上供编译器运行,但可以编译针对任何平台的代码)。

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). 这将有可能让你排序为C#6 / VB 14.还有最后一步获得VB 15:编辑langversion在web.config文件中,以便它读取15.0 (或者latest ,如果你想上的最新版本-这在我的首选方案中)。

<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. 不幸的是,我发现自己必须在将我的Roslyn升级到C#7.0然后再升级到7.1之后编辑langversion ,所以它绝对值得注意。

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. 顺便说一句,这个答案不仅适用于项目 - 如果使用的模板没有引用Nuget编译器软件包,即使在VS 2017中创建的新Web项目也需要此修复。 Roslyn is not mandatory and the template for eg a new Web Site has reportedly not been updated. Roslyn不是强制性的,据报道,例如新网站的模板未被更新。

The solutions was quite easy: 解决方案非常简单:

  • Update Nuget packages for solution, install "Microsoft.CodeDom.Providers.DotNetCompilerPlatform". 更新解决方案的Nuget包,安装“Microsoft.CodeDom.Providers.DotNetCompilerPlatform”。 That will also install Microsoft.Net.Compilers 这也将安装Microsoft.Net.Compilers
  • Then, upgrade the version of Microsoft.Net.Compilers to the latest stable version 然后,将Microsoft.Net.Compilers的版本升级到最新的稳定版本

And this is the solution to get rid of the red squiggles underneath any language construct that Visual Studio 2017 thinks is unsupported. 这是摆脱Visual Studio 2017认为不支持的任何语言结构下面的红色波形的解决方案。

The web.config contained this: web.config包含:

    <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=\&quot;Web\&quot; /optionInfer+"/>
     </compilers>
   </system.codedom>

in which /langversion:default had to be replaced with /langversion:14.0 like this: 其中/langversion:default必须用/langversion:14.0替换,如下所示:

    <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=\&quot;Web\&quot; /optionInfer+"/>
     </compilers>
   </system.codedom>

I found a reference to this on https://github.com/aspnet/RoslynCodeDomProvider/issues/16 我在https://github.com/aspnet/RoslynCodeDomProvider/issues/16上找到了对此的引用

This worked for me -- see /langversion:latest below 这对我/langversion:latest - 见/langversion:latest下面

<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=\&quot;Web\&quot; /optionInfer+"/>
  </compilers>
</system.codedom>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM