adinas
adinas

Reputation: 4550

How to use C# 6.0 or 7.0 in an old ASP.NET Website (not Website Project)

I have multiple old ASP.NET webforms websites (this is how to create one from scratch): enter image description here

They are not Website Projects.

ASP.NET Websites seem to be stuck at C# 5. When trying to use features from c# 6 or C# 7 you get an error: enter image description here Trying to use the light bulb upgrade option fails: enter image description here Trying any of the other options found at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version fail too since there is no .csproj file. or an option in the Project Properties page to change the language version.

Here is what the Build tab looks like on an ASP.NET Website (no Advanced button): enter image description here

I installed the Microsoft.Net.Compilers NuGet package but that made no difference.

Any Ideas if this can be done at all in an ASP.NET website?

Upvotes: 11

Views: 3519

Answers (3)

akshayblevel
akshayblevel

Reputation: 384

Right click on the project, click on properties, select build tab, click on Advance button at right bottom, select Language Version and click on OK button.

enter image description here

Upvotes: 6

Scott Baker
Scott Baker

Reputation: 10453

If you install the Microsoft.CodeDom.Providers.DotNetCompilerPlatform it will update your web.config file to use the current version - right now it's on 7.3:

  <system.codedom>
    <compilers>
      <compiler extension=".cs" language="c#;cs;csharp" warningLevel="4" 
        compilerOptions="/langversion:7.3 /nowarn:1659;1699;1701;612;618" 
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, 
              Microsoft.CodeDom.Providers.DotNetCompilerPlatform, 
              Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      <compiler extension=".vb" language="vb;vbs;visualbasic;vbscript" warningLevel="4" 
        compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" 
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, 
              Microsoft.CodeDom.Providers.DotNetCompilerPlatform, 
              Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </compilers>
  </system.codedom>

Upvotes: 12

ice6
ice6

Reputation: 1183

I run into a project, it is an old asp.net project without .csproj file.

If you create a new asp.net website, you will find the .csproj file, and you can use C# version above 5 happily.

So the problem become: how to generate .csproj file for the old project.

Just create a new project, and copy the code from old project inside Visual Studio. And then copy the new .csproj to the old project. And then your old asp.net project refreshed.:)

Upvotes: 2

Related Questions