【问题标题】:SignalR/NetCore3.1/React.Js ~/negotiate?negotiateVersion=1 404 (Not Found) (Believe its CORS issue)SignalR/NetCore3.1/React.Js ~/negotiate?negotiateVersion=1 404 (Not Found) (相信它的 CORS 问题)
【发布时间】:2020-05-14 03:27:25
【问题描述】:

在下面不断收到此错误:

localhost:3000/negotiate?negotiateVersion=1 404(未找到) 错误:无法完成与服务器的协商:错误:未找到 错误:无法启动连接:错误:未找到

这是软件版本

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="5.0.0-``
    <PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0" />
  </ItemGroup>

---------客户--------

  "dependencies": {
    "@microsoft/signalr": "^3.1.4",

这是我的 Cors.cs。我试过不包括 .WithOrigins 或更改端口,但它没有帮助。

    public static void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAllCors", builder =>
            {
                builder

                .WithOrigins("https://localhost:3000")
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials()
                .SetIsOriginAllowedToAllowWildcardSubdomains()
                .SetIsOriginAllowed(delegate (string requestingOrigin)
                {
                    return true;
                }).Build();
            });
        });
    public static void Configure(IApplicationBuilder app)
    {
        app.UseCors("AllowAllCors");
        app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/client/messages");
        });
    }

--

import React, { Component } from 'react';
import * as signalR from '@microsoft/signalr';

class EmailEditor extends Component {
  constructor(props) {
    super(props);

    this.state = {
      nick: '',
      message: '',
      messages: [],
      hubConnection: null,
    };
  }
  componentDidMount = () => {
    const nick = window.prompt('Your name:', 'John');

    const hubConnection = new signalR.HubConnectionBuilder()
      .withUrl("/client/messages")
      .configureLogging(signalR.LogLevel.Information)
      .build();

    this.setState({ hubConnection, nick }, () => {
      this.state.hubConnection.start()
        .then(() => console.log('Connection started!'))
        .catch(err => console.log('Error while establishing connection :('+ err));

      this.state.hubConnection.on('ReceiveMessage', (nick, receivedMessage) => {
        const text = `${nick}: ${receivedMessage}`;
        const messages = this.state.messages.concat([text]);
        this.setState({ messages });
      });
    });
  };

【问题讨论】:

  • 在 .NET Core 上,您不需要 SignalR 包,因为 SignalR 在 .NET Core 3.1 上是本机的。

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


【解决方案1】:

服务器的端口在 5001,因此它必须是

const hubConnection = new signalR.HubConnectionBuilder()
      .withUrl("https://localhost:5001/client/messages")
      .configureLogging(signalR.LogLevel.Information)
      .build();

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 2017-12-23
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多