如何将 C# 6 与网站项目类型一起使用?
How to use C# 6 with Web Site project type?
更新了现有的网站项目类型Visual Studio 2015,我将框架更改为4.6。
然后我希望在我的代码隐藏文件中提供所有这些新功能。
不幸的是,我收到如下错误:
Error CS8026: Feature 'expression-bodied property' is not available in C# 5. Please use language version 6 or greater.
或例如:
Error CS8026: Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater.
我快速 Google 检查并在 ScottGu 的一篇博文中找到 a guy posting some comments(在页面上搜索“8026”)。
由于我不理解他的解决方案,而且我想让解决方案更加明显,所以我创建了这个 SO 帖子。
我的问题:
如何让类型为 Web 站点 的 Visual Studio 2015 项目(即不是 Web 应用程序)识别 C# 6 个特征?
我已经用 ASP.NET MVC 5(测试过 5.2.3)对此进行了测试,您的里程可能会因其他 Web 框架而异,但您只需要添加 NuGet package for Roslyn CodeDOM.
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
应该添加 DLL 文件...
PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
Replacement CodeDOM providers that use the new .NET Compiler Platform ("Roslyn") compiler as a service APIs. This provides support for new language features in systems using CodeDOM (e.g. ASP.NET runtime compilation) as well as improving the compilation performance of these systems.
...并将以下内容添加到您的 web.config:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
如果仍然缺少 XML,请尝试自己添加。
可能的解决方案,取自the comments on ScottGu's blog posting(在页面上搜索“8026”):
解决方案建议1(David Taylor)
将这些 RTM 代码 dom 元素添加到 web.config:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
然后将 Roslyn 和 Microsoft.CodeDom.Providers.*.dll 添加到您的 BIN 目录中。
设置起来很容易,但令我感到奇怪的是,如果您在 VS 2015 RTM 中创建新 "Web Site" 时选择 .NET 4.6,则没有为其设置默认模板。
解决建议2(马丁)
根据上面 David Taylor 的评论,当使用 TargetFramework 作为默认 v4.5.2 创建 Web 应用程序时,system.codedom 设置似乎是正确的。将 TargetFramework 更改为 v4.6 似乎会以导致问题的方式修改 CSharp 的编译器设置。
我的解决方法如下:
- File/New/ASP.NET Web 应用程序
- Select "Web API" 模板来自 ASP.NET 4.5.2 模板
- 在 web.config
中复制 system.codedom 元素(及其内容)
- 使用 Properties/TargetFramework,将目标框架设置为 4.6
- 将 web.config 中修改后的 system.codedom 元素替换为更改 TargetFramework
之前的副本
- 点击F5
主页应按预期加载。
有关信息,将 TargetFramework 更改为 v4.6 后的 system.codedom 内容。如下(注意使用类型"Microsoft.CSharp.CSharpCodeProvider"):
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701">
<providerOption name="CompilerVersion" value="v4.0"/>
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
我按照@jbtule 的建议安装了 DotNetCompilerPlatform,但仍然出现相同的错误。
PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
我关闭了解决方案,删除了 bin 和 obj 文件夹,然后打开解决方案并重建。现在 C# 6 功能可以使用了。
另请参阅 this blog post from Sayed Ibrahim Hashimi,了解如何通过 VS IDE 执行此操作。
In Visual Studio 2015 Update 1 we have included a new feature to simplify this. 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.
更新。
VS 2017 和 2019 已将此功能移至 Build -> ASP.NET Compilation。
我在对我的 WebAPI 项目进行零更改后收到此错误。我卸载了 DotNetCompilerPlatform nuget 包并重新安装,这解决了问题。
这不适用于网站项目。这就是 ASP.NET MVC 项目的处理方式。
您可以在 Visual Studio UI 中切换 C# 6。这是一个如此简单的选择,值得一试。
- 右键单击您的项目并select 属性。
- 单击构建 选项卡。
- 构建选项卡的最底部有一个 高级... 按钮。
- 这将打开 高级构建设置,如下所示。 Select C# 6.0.
下面是一个网站项目在 VS2017 中的样子:
我在 VS2019 Visual Studio 2019 中偶然发现了这个搜索解决方案。
这是在您的解决方案文件中选择了网站项目。 “构建 → ASP.NET 编译 → 启用最新的 C# 和 VB 语言功能”。
更新了现有的网站项目类型Visual Studio 2015,我将框架更改为4.6。
然后我希望在我的代码隐藏文件中提供所有这些新功能。
不幸的是,我收到如下错误:
Error CS8026: Feature 'expression-bodied property' is not available in C# 5. Please use language version 6 or greater.
或例如:
Error CS8026: Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater.
我快速 Google 检查并在 ScottGu 的一篇博文中找到 a guy posting some comments(在页面上搜索“8026”)。
由于我不理解他的解决方案,而且我想让解决方案更加明显,所以我创建了这个 SO 帖子。
我的问题:
如何让类型为 Web 站点 的 Visual Studio 2015 项目(即不是 Web 应用程序)识别 C# 6 个特征?
我已经用 ASP.NET MVC 5(测试过 5.2.3)对此进行了测试,您的里程可能会因其他 Web 框架而异,但您只需要添加 NuGet package for Roslyn CodeDOM.
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
应该添加 DLL 文件...
PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
Replacement CodeDOM providers that use the new .NET Compiler Platform ("Roslyn") compiler as a service APIs. This provides support for new language features in systems using CodeDOM (e.g. ASP.NET runtime compilation) as well as improving the compilation performance of these systems.
...并将以下内容添加到您的 web.config:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
如果仍然缺少 XML,请尝试自己添加。
可能的解决方案,取自the comments on ScottGu's blog posting(在页面上搜索“8026”):
解决方案建议1(David Taylor)
将这些 RTM 代码 dom 元素添加到 web.config:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
然后将 Roslyn 和 Microsoft.CodeDom.Providers.*.dll 添加到您的 BIN 目录中。
设置起来很容易,但令我感到奇怪的是,如果您在 VS 2015 RTM 中创建新 "Web Site" 时选择 .NET 4.6,则没有为其设置默认模板。
解决建议2(马丁)
根据上面 David Taylor 的评论,当使用 TargetFramework 作为默认 v4.5.2 创建 Web 应用程序时,system.codedom 设置似乎是正确的。将 TargetFramework 更改为 v4.6 似乎会以导致问题的方式修改 CSharp 的编译器设置。
我的解决方法如下:
- File/New/ASP.NET Web 应用程序
- Select "Web API" 模板来自 ASP.NET 4.5.2 模板
- 在 web.config 中复制 system.codedom 元素(及其内容)
- 使用 Properties/TargetFramework,将目标框架设置为 4.6
- 将 web.config 中修改后的 system.codedom 元素替换为更改 TargetFramework 之前的副本
- 点击F5
主页应按预期加载。
有关信息,将 TargetFramework 更改为 v4.6 后的 system.codedom 内容。如下(注意使用类型"Microsoft.CSharp.CSharpCodeProvider"):
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701">
<providerOption name="CompilerVersion" value="v4.0"/>
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
我按照@jbtule 的建议安装了 DotNetCompilerPlatform,但仍然出现相同的错误。
PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
我关闭了解决方案,删除了 bin 和 obj 文件夹,然后打开解决方案并重建。现在 C# 6 功能可以使用了。
另请参阅 this blog post from Sayed Ibrahim Hashimi,了解如何通过 VS IDE 执行此操作。
In Visual Studio 2015 Update 1 we have included a new feature to simplify this. 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.
更新。
VS 2017 和 2019 已将此功能移至 Build -> ASP.NET Compilation。
我在对我的 WebAPI 项目进行零更改后收到此错误。我卸载了 DotNetCompilerPlatform nuget 包并重新安装,这解决了问题。
这不适用于网站项目。这就是 ASP.NET MVC 项目的处理方式。
您可以在 Visual Studio UI 中切换 C# 6。这是一个如此简单的选择,值得一试。
- 右键单击您的项目并select 属性。
- 单击构建 选项卡。
- 构建选项卡的最底部有一个 高级... 按钮。
- 这将打开 高级构建设置,如下所示。 Select C# 6.0.
下面是一个网站项目在 VS2017 中的样子:
我在 VS2019 Visual Studio 2019 中偶然发现了这个搜索解决方案。
这是在您的解决方案文件中选择了网站项目。 “构建 → ASP.NET 编译 → 启用最新的 C# 和 VB 语言功能”。