关于 Visual Studio:在 VS 2015 社区版中将 .Net 框架从 4.5.2 更改为 4.5 时出现编译错误

Compilation Error when change .Net framework from 4.5.2 to 4.5 in VS 2015 Community edition

在 VS 2015 社区版中将我的 .Net 框架从 4.5.2 更改为 4.5 之后。我收到以下错误。有谁知道问题是什么以及如何解决这个问题。看起来编译版本不同,但我已经将我的 web.config 修改为 4.5

Server Error in '/' Application.

Compilation Error Description: An error occurred during the
compilation of a resource required to service this request. Please
review the following specific error details and modify your source
code appropriately.

Compiler Error Message: CS1617: Invalid option '6' for /langversion;
must be ISO-1, ISO-2, 3, 4, 5 or Default

Source Error:

[No relevant source lines]

Source File: Line: 0

Show Detailed Compiler Output:

C:\\Program Files (x86)\\IIS Express>
"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe" /t:library
/utf8output /R:"C:\\Users\\admin\\AppData\\Local\\Temp\\Temporary ASP.NET
Files\
oot\\479d40ee\\add72695\\assembly\\dl3\\31bb6ea7\\6a8168a8_dae8d001\\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll"
/R:"C:\\WINDOWS\\Microsoft.Net\\assembly\\GAC_MSIL\\System.Configuration\\v4.0_4.0.0.0__b03f5f7f11d50a3a\\System.Configuration.dll"
/R:"C:\\WINDOWS...................

Microsoft (R) Visual C# Compiler version 4.6.0079.0

for C# 5 Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework,
but only supports language versions up to C# 5, which is no longer the
latest version. For compilers that support newer versions of the C#
programming language, see
http://go.microsoft.com/fwlink/?LinkID=533240

error CS1617: Invalid option '6' for /langversion; must be ISO-1,
ISO-2, 3, 4, 5 or Default


我回答我的问题只是为了让其他用户知道解决方案是什么。经过一些谷歌搜索和个人研究。所以我正好做了两件事。

  • 转到每个项目属性 -> 构建 -> 高级并更改
    语言版本到 C# 5.0
  • 打开启动项目的 web.config(在我的例子中是 MVC)并删除
    包含自定义编译信息的 codedom 部分

这解决了我的问题。


Visual Studio 2015 中的 C# 是 C# 6.0,但是您收到的错误消息来自 ASP.NET 编译器(不是 Visual Studio 编译器),通知您 C# 6.0 不可用,只有 C# 最新版本5,所以不是版本 6。

我怀疑此错误消息来自您的虚拟主机提供商,这意味着他们尚未在其系统上安装最新版本的 .NET Framework,您需要在您的系统中降级 C# 语言编译选项web.config 恢复到以前的状态,并避免在运行时编译的代码中使用任何 C# 6.0 语言功能。如果依赖任何新的 CLR 指令,我不知道在 .NET 4.5.0 环境中运行 C# 6.0 语言功能会有什么影响。

更新:

或者,确保您使用的是最新版本的 Microsoft.Net.Compilers NuGet 包,这会导致在部署时将最新版本的 csc 与您的项目捆绑在一起,因此它将支持 C# 6、7 , 8 等


这个解决方案也对我有用。
窗口 10
视觉工作室 2013
.Net 框架 4.5

转到每个项目属性 -> 构建 -> 高级并将语言版本更改为 C# 5.0
打开启动项目的 web.config(在我的例子中是 MVC)并删除包含自定义编译信息的 codedom 部分
这解决了我的问题。


我正面临着使用 MSBuild 的同一栋建筑,以下帮助了我:

在 Visual Studio > 选择您的项目 > 右键单击?? > 属性 > 库 > 目标 > 更改 > .Net Framework 4.6

这个链接帮助了我