2017-10-05 95 views
6

在.cshtml視圖中使用插值字符串會導致以下intellisense錯誤:Feature 'interpolated strings' is not used in C#5. Please use language version 6 or greater.此和其他C#7語言功能在編譯代碼(.cs文件)中工作, 。VS2017 - C#7語言功能在MVC視圖中不起作用

正如您在下面看到的,最新的主要版本 C#7。

enter image description here

根據this question評論,「默認」 的意思是 「最新的主要版本」

那麼爲什麼錯誤呢?另外,爲什麼它沒有顯示「最新主要版本」和「最新次要版本」作爲單獨的列表選項,正如我在許多在線示例中看到的那樣?

更新:

我終於設法通過安裝CodeDom providers package(該Microsoft.Net.Compilers包拿到串​​插在我的意見的工作是有關的MSBuild CodeDOM的供應商包與ASP.NET和其他apis在運行時編譯,因此即使爲msbuild選擇了最新的語言版本,爲什麼cshtml文件也會顯示錯誤(如果未安裝該文件)。這種自動添加以下到我的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.7.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.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 

編號:C# 6.0 Features Not Working with Visual Studio 2015

升級到MVC6也有固定的,我相信。

+3

您正在使用哪個版本的VS2017?你的截圖看起來讓人聯想到RC版本;不是發佈的版本。 –

+0

@DavidArno - Enterprise。 – Kev

+0

對不起,我的意思是如15.3.5中的版本。你可以通過'幫助 - >關於Microsoft Visual Studio'來找到答案。 –

回答

4

我懷疑你的問題的關鍵是當你說「在視圖中」。假設這是在ASP.Net的上下文中,你應該看看你的web.config,它可以指定它自己的LangVersion設置(可能在你的情況下硬編碼爲5)。

有關配置ASP.Net以使用更新版本的C#的更多詳細信息可以在Roslyn documentation issue中找到。

+0

沒有LangVerson在我的web.config。我嘗試按照鏈接頁面中的建議安裝Microsoft.Net.Compilers(儘管我不應該在VS2017中)。仍然沒有LangVersion。仍然給錯誤。 – Kev

+0

你可以分享你的web.config嗎? 應該是這樣的: '' –

+0

不,沒有這樣的事情。我可以在.cs文件中使用字符串插值,但不能使用.cshtml文件。 – Kev