ASP.NET 核心没有 app.UseEndpoints() 方法

ASP.NET CORE Don't have app.UseEndpoints() Method

现在正在学习 ASP.NET Core,在一些指南中我看到了 app.UseEndpoints() 方法。

但是当我创建 ASP NET CORE 项目时,我只在 StartUp.cs

中看到 app.Run
  1. 所以我需要为此安装一些实用程序还是删除了 UseEndPoints?
  2. 如何实现这个方法 app.UseEndpoints(endpoints => { endpoints.MapHub<ChatHub>("/chat"); });

如果您使用的是 .NET Core 3.1 版本,那么您需要确保您拥有:

using Microsoft.AspNetCore.Builder;

在文件中,您需要(直接或间接)引用 Microsoft.AspNetCore.Routing 程序集。

如果您正在学习,最好从当前的 .NET Core 版本 3.1 开始。 2.1 根本没有端点路由,端点路由的工作始于 2.2,但我认为它主要是在幕后,没有像 UseEndpoints() 那样暴露给消费者代码。在 3.1 中,Sean 的回答适用 - 您通常只需在 csproj 中指定 <Project Sdk="Microsoft.NET.Sdk.Web"> 即可获得正确的 NuGet 包含。

如果您使用的是 Net Core 2.1,则必须这样配置:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SignalRChat.Hubs;

namespace SignalRChat
{


public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }


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


    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/chat");
        });
        app.UseMvc();
    }
}

}

只有 3.0 版本以后才能使用 app.UseEndpoints

app.UseEndpoints(endpoints =>
        {              
            endpoints.MapHub<ChatHub>("/chat");
        });

查看文档:

ASP.NET Core 2.1

ASP.NET Core 3.0 +