如何将 ASP.NET Core 2.1 升级到 ASP.NET Core 3.1 版本

作者 : 慕源网 本文共3818个字,预计阅读时间需要10分钟 发布时间: 2021-10-20 共882人阅读

介绍

本文提供了将 ASP.NET Core 2.1/2.0(或更低版本)升级到 3.1 的指南。.Net Core 3.1 有长期支持,如果您使用的是 2.0/2.1 或更低版本的 .NET Core 应用程序并且需要升级它,这篇文章将对您有所帮助。

迁移的第一步是更改目标框架。为此:右键单击项目-> 属性,然后在项目属性中选择目标框架为 3.1,如下图所示。

或者,您可以将目标框架从 .csproj 文件更改为netcoreapp3.1

右键单击项目,然后单击编辑项目文件。

然后将目标框架更改为netcoreapp3.1

从解决方案属性将 .net 核心版本从 2.1 更改为 3.1 后,然后构建解决方案。某些软件包将自动恢复和升级。您可能会在解决方案中遇到许多错误,但随之而来的是一些建议,显示升级解决方案框架后您需要做什么。根据建议,您可以解决问题;但是,在本文中,我们基本上将讨论 Startup。cs 文件更改非常重要,即使您在构建解决方案后也没有错误,也会抛出错误。

准则 1

即使您的解决方案成功构建,您在运行应用程序时也可能会收到以下错误。

当我们继续运行项目时,页面将加载以下错误消息。

让我们转到上述错误的解决方案。事情是在迁移到 ASP.NET Core 3.1 之后,我们必须使用 UseEndPoints() 而不是 UseMVC();

在  Startup.cs 文件的Configuration() 方法中,我们需要更改以下代码。

app.UseMvc(routes => {
    routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
});

app.UseEndpoints(endpoints => {
    endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});

准则 2

您可能会收到以下错误,

错误:EndpointRoutingMiddleware 匹配 EndpointMiddleware 设置的端点,因此必须在 EndpointMiddleware 之前添加到请求执行管道

如果您收到上述错误,请按照以下建议操作。

我们必须使用 app.UseRouting();在 Startup.cs 文件的 Configuration()方法中

app.UseRouting();//error message suggested to implement this

准则 3

此外,如果您已经实现了 SignalR,那么您将收到如下信息或警告消息:

在 ASP.NET Core 2.1 应用程序中,SignalR 如下所示编写,它驻留在 Startup.cs 文件的 Configuration() 方法中。

app.UseSignalR(routes => {
    routes.MapHub < ChatUpdaterHub > ("/chat-updater");
});

但是,在 ASP.NET Core 3.1 中,它应该使用端点实现,如下面的代码所示。

app.UseEndpoints(endpoints => {
    endpoints.MapHub < ChatUpdaterHub > ("/chat-updater"); //Signal R implementation
    endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});

.NET Core 2.1 的 Startup.cs 类的代码是

public class Startup {
    public IConfiguration Configuration {
        get;
    }
    public Startup(IConfiguration configuration) {
        this.Configuration = configuration;
    }
    public void ConfigureServices(IServiceCollection services) {
        services.AddSingleton(defaultEndpointsSettings);
        services.AddDistributedMemoryCache();
        services.AddTransient < ApiRequester > ();
        services.AddHostedService < FetchingBackgroundService > ();
        services.AddMvc();
        services.AddSignalR();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
        if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
        } else {
            app.UseExceptionHandler("/Home/Error");
        }
        var cachePeriod = env.IsDevelopment() ? "600" : "604800";
        app.UseStaticFiles(new StaticFileOptions {
            OnPrepareResponse = ctx => {
                ctx.Context.Response.Headers.Append("Cache-Control", $ "public, max-age={cachePeriod}");
            }
        });
        app.UseSignalR(routes => {
            routes.MapHub < DataUpdaterHub > ("/ws-updater");
        });
        app.UseMvc(routes => {
            routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

当我们将同一个项目升级到 .Net Core 3.1 Startup.cs 类时应该是这样的,

public class Startup
   {
       public IConfiguration Configuration { get; }
       public IWebHostEnvironment Environment { get; }
       public Startup(IConfiguration configuration, IWebHostEnvironment environment)
       {
           this.Configuration = configuration;
           Environment = environment;
       }

       public void ConfigureServices(IServiceCollection services)
       {

           services.AddSingleton(defaultEndpointsSettings);
           services.AddDistributedMemoryCache();
           services.AddTransient<ApiRequester>();
           services.AddHostedService<FetchingBackgroundService>();
           services.AddMvc();
           services.AddSignalR();

       }

       public void Configure(IApplicationBuilder app)
       {
           if (Environment.IsDevelopment())
           {
               app.UseDeveloperExceptionPage();
           }
           else
           {
               app.UseExceptionHandler("/Home/Error");
           }

           var cachePeriod = Environment.IsDevelopment() ? "600" : "604800";
           app.UseStaticFiles(new StaticFileOptions
           {
               OnPrepareResponse = ctx =>
               {
                   ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
               }
           });
           app.UseRouting();

           app.UseEndpoints(endpoints =>
           {
               endpoints.MapHub<DataUpdaterHub>("/ws-updater");
               endpoints.MapControllerRoute(
                   name: "default",
                   pattern: "{controller=Home}/{action=Index}/{id?}");
           });
       }
   }

微软推荐

Microsoft 对迁移到 .NET Core 3.1 的建议是:

  • 添加用户路由()
  • UseStatisFiles()、UseRouting()、UseAuthentication() 和 UseAuthorization()、UseCors() 和 UseEndPoints() 的顺序应按以下顺序

结论

本文介绍了将 .NET Core 从 2.1(或更低版本)升级到 3.1 的指南,包括示例代码和错误示例以及如何解决这些问题。我希望它可以帮助您在长期支持下将您的应用程序从较低的框架更新到较高的框架。

 


慕源网 » 如何将 ASP.NET Core 2.1 升级到 ASP.NET Core 3.1 版本

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录