.Net Core 2.1 React 和 Signalr 1.0.4 抛出 /negotiate 404 错误

.Net Core 2.1 React and Signalr 1.0.4 throwing a /negotiate 404 error

我目前正在使用 React 和 SignalR。我正在使用 .Net Core 2.1 和 Microsoft.AspNetCore.App 包来获取最新的 SignalR。我也安装了@aspnet/signalr,但我一直收到 404 错误,因为它仍在尝试转到我知道它不再使用的 /negotiate 端点。我已确认我了解所有包裹的最新信息。关于我应该去哪里的任何指示?

    const hubConnection = new HubConnectionBuilder().withUrl('http://localhost:3000/ChatHub').build();

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

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
            //{
            //    builder
            //        .AllowAnyMethod()
            //        .AllowAnyHeader()
            //        .WithOrigins("http://localhost:3000");
            //}));

            services.AddSignalR();
            //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            //app.UseCors("CorsPolicy");

            app.UseFileServer();

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

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();

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

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseReactDevelopmentServer(npmScript: "start");
                }
            });
        }

I keep getting a 404 error because it is still attempting to go to the /negotiate endpoint which I know it doesn't use anymore

ASP.NET 核心 SignalR 肯定仍然使用 /negotiate 端点。在预览中,我们使用了 OPTIONS 请求,但这导致了很多问题,所以我们回到了 /negotiate 端点。

您似乎在使用 "development server"(在您的 Startup 中使用 spa.UseReactDevelopmentServer)。这通常意味着开发服务器正在从 不同的服务器 提供您的 HTML/JS 内容,而不是 ASP.NET 核心应用 运行 在(而不是只是 ASP.NET 核心应用程序提供的静态文件)。如果是这种情况,您需要在连接时使用完整的 URL 引用 ASP.NET 核心服务器。

这是因为您的 HTML/JS 内容由位于 http://localhost:X 的开发服务器提供,但您的 ASP.NET 核心服务器位于 http://localhost:Y 运行。因此,当您将 /ChatHub 用作 URL 时,浏览器将其解释为 http://localhost:X/ChatHub,因此您不会点击 ASP.NET 核心应用程序(使用 SignalR 服务器),而是然后是开发服务器,它在 URL 处没有内容并产生 404.