How to use C# 6 with Web Site project type?

前端 未结 7 2121
说谎
说谎 2020-11-30 22:23

Updated an existing Web Site project type Visual Studio 2015, I changed the Framework to 4.6.

I then expected to have all those new features available in my

相关标签:
7条回答
  • 2020-11-30 22:37

    See also this blog post from Sayed Ibrahim Hashimi on how to do this through the 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.

    Update.

    VS 2017 and 2019 have this feature moved to Build -> ASP.NET Compilation.

    0 讨论(0)
  • 2020-11-30 22:42

    I installed the DotNetCompilerPlatform as @jbtule suggested, but was still getting the same errors.

    PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
    

    I closed the solution, deleted the bin and obj folders, then opened the solution and rebuilt. Now the C# 6 features work.

    0 讨论(0)
  • 2020-11-30 22:42

    I received this error after making ZERO changes to my WebAPI project. I uninstalled the DotNetCompilerPlatform nuget package and reinstalled, which fixed the problem.

    0 讨论(0)
  • 2020-11-30 22:47

    I've tested this with ASP.NET MVC 5 (tested 5.2.3), and your mileage may vary with other web frameworks, but you just need to add the Roslyn CodeDOM| NuGet package

    CodeDOM Providers for .NET Compiler...

    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.

    PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
    

    https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/

    The NuGet package should add the DLL files and add the following to your 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=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
      </system.codedom>
    

    If it's still missing. Try adding yourself.

    0 讨论(0)
  • 2020-11-30 22:56

    Possible solutions, taken from the comments on ScottGu's blog posting (search for "8026" on the page):

    Solution suggestion 1 (David Taylor)

    Add these RTM code dom elements to 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=\&quot;Web\&quot; /optionInfer+"/>
        </compilers>
    </system.codedom>
    

    Then add the Roslyn and Microsoft.CodeDom.Providers.*.dll stuff into your BIN directory.

    It is easy enough to setup, but just was strange to me that the default template wasn't set up for it if you selected .NET 4.6 when creating a new "Web Site" in VS 2015 RTM.

    Solution suggestion 2 (Martin)

    Further to David Taylor's comment above, it looks like the system.codedom settings are correct when a Web App is created with the TargetFramework as the default v4.5.2. Changing the TargetFramework to v4.6 appears to modify the compiler settings for CSharp, in a way that causes an issue.

    My workarounds was as follows:

    1. File/New/ASP.NET Web Application
    2. Select "Web API" template from ASP.NET 4.5.2 Templates
    3. Take a copy of the system.codedom element (and its contents) in web.config
    4. Using Properties/TargetFramework, set the Target Framework to 4.6
    5. Replace the modified system.codedom element in web.config with the copy taken prior to changing TargetFramework
    6. Hit F5

    Home Page should load as expected.

    For info, the system.codedom contents immediately after changing TargetFramework to v4.6. was as follows (note use of the Type "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=\&quot;Web\&quot; /optionInfer+"/>
        </compilers>
    </system.codedom>
    
    0 讨论(0)
  • 2020-11-30 22:57

    This is not for a Web Site Project. This is how you do it for an ASP.NET MVC project.

    You can toggle C# 6 in the Visual Studio UI. This is such an easy option, it is worth trying first.

    1. Right click your project and select Properties.
    2. Click the Build tab.
    3. The build tab has an Advanced... button at the very bottom.
    4. This opens up the Advanced Build Settings as shown below. Select C# 6.0.

    0 讨论(0)
提交回复
热议问题