简体   繁体   中英

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): 在此处输入图片说明

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: 在此处输入图片说明 Trying to use the light bulb upgrade option fails: 在此处输入图片说明 Trying any of the other options found at https://docs.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): 在此处输入图片说明

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?

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.

Click to see screen shot

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.:)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM