本文介绍了如何将.NET Core 2.2 Web API迁移到.NET Core 3.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注,但失败了。因此,我改为使用.NET 2.2创建了一个新项目以建立干净的模板,然后尝试迁移到.NET Core 3.0。但是它再次失败了。在 Startup.cs 中,依赖项注入找不到方法 services.AddMvc()

I was following Microsoft's migration guide, but failed. So, instead, I created a new project with .NET 2.2 to establish a clean template, and then tried migrating to .NET Core 3.0. But it once again failed. In the Startup.cs, the dependency injection cannot find the method services.AddMvc().

似乎我的代码仍然引用.NET 2.2而不是.NET 3.0。

It seems my code still refers to .NET 2.2 instead of .NET 3.0.

如果我运行运行dotnet watch ,出现以下错误:

If I run dotnet watch run, I get the following error:

。我的.NET信息:

The full code I am running can be found on GitHub. My .NET info:

运行时环境:操作系统名称:Windows操作系统版本:
10.0.18362操作系统平台:Windows RID:win10-x64 Base路径:C:\Program Files\dotnet\sdk\3.0.100\

Runtime Environment: OS Name: Windows OS Version: 10.0.18362 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.0.100\

主机(用于支持):版本:3.0.0提交: 7d57652f33

Host (useful for support): Version: 3.0.0 Commit: 7d57652f33

已安装.NET Core SDK:
2.2.401 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]

.NET Core SDKs installed: 2.2.401 [C:\Program Files\dotnet\sdk] 3.0.100 [C:\Program Files\dotnet\sdk]

已安装.NET Core运行时:Microsoft.AspNetCore.All 2.2.6
[C: \程序文件\dotnet\共享\Microsoft.AspNetCore.All]

Microsoft.AspNetCore.App 2.2.6 [C:\程序
文件\dotnetb共享\Microsoft.AspNetCore.App]

Microsoft.AspNetCore.App 3.0.0 [C:\Program
文件\dotnet\共享\Microsoft.AspNetCore.App] Microsoft。 ñ ETCore.App
2.2.6 [C:\Program Files\dotnet\SharedMicrosoft.NETCore.App] Microsoft.NETCore.App 3.0.0 [C:\Program
文件共享网络Microsoft.NETCore.App]

Microsoft.WindowsDesktop.App 3.0.0 [C:共享程序
文件共享Microsoft。 WindowsDesktop.App]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

要安装其他.NET Core运行时或SDK:

To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download


推荐答案

对于v2.2

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }

对于v3

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

还要确保您的csproj是目标.net core 3.0

Also make sure your csproj is target .net core 3.0

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

这篇关于如何将.NET Core 2.2 Web API迁移到.NET Core 3.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:39