Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper issue with C# 6.0 features in cshtml files

I've just set up a new PC with VS2017 Pro (15.2) and Resharper (2017.1.2), on Windows 10 (1703).

I've opened an existing solution, and Resharper has highlighted a load of issues in CSHTML files. The ones I've looked at are all string interpolation and null conditional operators, and the message is "C# 6.0 language feature", as though this is an error. The projects all still compile and run OK, and string interpolation and null conditional operators aren't highlighted as errors in normal CS files.

Errors/Warnings in Solution Razor Editor

The projects in the solution are all set to Framework 4.6.1, and the C# Language Level was set to Default. I've set the LanguageLevel to CSharp60 in the sln.DotSettings file and the projects now all show the language level as C# 6.0, but Resharper is still moaning about the string interpolation errors. Clearing the Resharper cache hasn't helped.

The solution is running on another recently installed PC with no errors at all. The only difference is that on the other PC it's a completely new build, whereas this build is a new OS on a new C:\ drive, but the code is on a second drive that hasn't been changed.

The web.config in my views folder looks like this:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="Az.Ems.Web" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

And the codedom section of the main web.config looks like this

 <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /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.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
like image 550
Jonathan Sayce Avatar asked Jun 13 '17 15:06

Jonathan Sayce


2 Answers

Try setting the compiler flags to /langversion:6 instead of default. ReSharper appears to be trying to parse the version as a number, which will fail and keep ReSharper at C# 5.

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" 
              extension=".cs"
              type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.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.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              warningLevel="4"
              compilerOptions="/langversion:6 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
</system.codedom>
like image 114
citizenmatt Avatar answered Oct 19 '22 08:10

citizenmatt


In addition to citizenmatt's answer, Resharper does not support the <location> tag. In such cases, you need to move the <system.codedom> block outside of the <location> block.

https://resharper-support.jetbrains.com/hc/en-us/community/posts/115000754490-Incorrect-warning-about-C-6-0-Language-Feature-in-editor

like image 2
stricq Avatar answered Oct 19 '22 10:10

stricq