【问题标题】:.Net Core 2.1 React and Signalr 1.0.4 throwing a /negotiate 404 error.Net Core 2.1 React 和 Signalr 1.0.4 抛出 /negotiate 404 错误
【发布时间】:2018-10-23 14:58:27
【问题描述】:

我目前正在使用 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");
                }
            });
        }

【问题讨论】:

    标签: reactjs asp.net-core-2.1 asp.net-core-signalr


    【解决方案1】:

    我不断收到 404 错误,因为它仍在尝试转到我知道它不再使用的 /negotiate 端点

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

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

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

    【讨论】:

    • 没问题!这是一个令人惊讶的普遍问题,所以不要难过!
    • @AndrewStanton-Nurse 嗨,我也面临同样的问题。在我的情况下,我的 .net core Angular 应用程序托管在“localhost:xxxx”中,但我的带有信号 R 集线器的 .NET Core api 在“localhost:yyyy/checkstatus”中,它们都在 IIS 中。那么,在 Angular 客户端使用 Hubconnection 的 .WithUrl() 从 .NET Core 应用程序调用信号 r api 需要进行哪些更改?我是否必须更改 AddSignalR() 中间件的启动文件?在这种情况下,HubConnectionBuilder().withUrl() 的 api url 应该是什么
    • 您不需要更改服务器。您只需要确保您的 client-side 代码使用服务器的完整 URL,而不是相对 URL。如果您的集线器位于https://localhost:yyyy/checkstatus,那么您将使用https://localhost:yyyy/checkstatus 作为.withUrl 中的网址
    • @AndrewStanton-Nurse:我使用的是开箱即​​用的设置,前端没有完整的 URL。我也在使用 React 开发服务器。我的 Web API 正在监听 localhost:5001/api,我的集线器在 localhost:5001/api/myHub,我看到一个发往 localhost:5001/api/myHub/negotiate 的 POST 请求,但响应是 404。
    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 2021-01-22
    • 1970-01-01
    • 2017-10-17
    • 2017-11-20
    • 1970-01-01
    • 2020-05-01
    • 2017-05-19
    相关资源
    最近更新 更多