简体   繁体   English

如何在旧的 ASP.NET 网站(不是网站项目)中使用 C# 6.0 或 7.0

[英]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):我有多个旧的 ASP.NET webforms 网站(这是从头开始创建一个的方法): 在此处输入图片说明

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: ASP.NET 网站似乎停留在 C# 5。尝试使用来自 c# 6 或 C# 7 的功能时,您会收到错误消息: 在此处输入图片说明 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.尝试在https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version 上找到的任何其他选项也失败,因为没有 .csproj 文件。 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):以下是 ASP.NET 网站上的“构建”选项卡的外观(无“高级”按钮): 在此处输入图片说明

I installed the Microsoft.Net.Compilers NuGet package but that made no difference.我安装了 Microsoft.Net.Compilers NuGet 包,但这没什么区别。

Any Ideas if this can be done at all in an ASP.NET website?如果这可以在 ASP.NET 网站中完成,有什么想法吗?

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.我遇到了一个项目,它是一个没有 .csproj 文件的旧 asp.net 项目。

If you create a new asp.net website, you will find the .csproj file, and you can use C# version above 5 happily.如果新建一个asp.net网站,就会找到.csproj文件,就可以愉快的使用C#5以上的版本了。

So the problem become: how to generate .csproj file for the old project.所以问题就变成了:如何为旧项目生成 .csproj 文件。

Just create a new project, and copy the code from old project inside Visual Studio.只需创建一个新项目,然后从 Visual Studio 中的旧项目复制代码。 And then copy the new .csproj to the old project.然后将新的 .csproj 复制到旧项目中。 And then your old asp.net project refreshed.:)然后你的旧 asp.net 项目刷新了。:)

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 :如果您安装Microsoft.CodeDom.Providers.DotNetCompilerPlatform ,它将更新您的web.config文件以使用当前版本 - 现在它在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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