Angular连接到ASP.NET API SignalR Hub返回404 Not Found(Identity Server 3)。
创始人
2024-10-28 00:00:47
0

问题描述: 在Angular应用程序中,尝试连接到ASP.NET API的SignalR Hub时,返回404 Not Found错误。

解决方法:

  1. 确保在Angular应用程序中正确安装和配置了SignalR模块。可以使用以下命令安装SignalR模块:
npm install @aspnet/signalr
  1. 在Angular应用程序的app.module.ts文件中导入SignalR模块,并将其添加到NgModule的imports数组中:
import { SignalRModule } from '@aspnet/signalr';

@NgModule({
  imports: [
    SignalRModule.forRoot()
  ],
  ...
})
export class AppModule { }
  1. 在需要使用SignalR的组件中导入SignalR服务,并在构造函数中注入它:
import { SignalRService } from './signalr.service';

@Component({
  ...
})
export class MyComponent {
  constructor(private signalRService: SignalRService) { }
}
  1. 创建一个名为SignalRService的服务,并在其构造函数中初始化SignalR连接:
import { Injectable } from '@angular/core';
import { HubConnection, HubConnectionBuilder } from '@aspnet/signalr';

@Injectable({
  providedIn: 'root'
})
export class SignalRService {
  private hubConnection: HubConnection;

  constructor() {
    this.hubConnection = new HubConnectionBuilder()
      .withUrl('http://your-api-url/signalr-hub') // 替换为你的API的SignalR Hub URL
      .build();

    this.hubConnection.start()
      .then(() => console.log('SignalR connection started'))
      .catch(err => console.log('Error while starting SignalR connection: ' + err));
  }
}

请确保将上述代码中的http://your-api-url/signalr-hub替换为你的API的SignalR Hub的URL。

  1. 确保在ASP.NET API项目中正确配置了SignalR Hub。在Startup.cs文件的ConfigureServices方法中添加以下代码:
services.AddSignalR();

Configure方法中添加以下代码:

app.UseSignalR(routes =>
{
    routes.MapHub("/signalr-hub"); // 替换为你的Hub类的名称
});

请确保将上述代码中的YourHubClass替换为你的Hub类的名称。

  1. 确保ASP.NET API项目中正确配置了Identity Server 3。请参考Identity Server 3的文档进行正确的配置。

  2. 确保API的SignalR Hub的URL在Identity Server 3的允许列表中。在Identity Server 3的配置文件中,添加以下代码:

public static IEnumerable GetClients()
{
    return new List
    {
        new Client
        {
            ...
            AllowedCorsOrigins = new List
            {
                "http://your-angular-app-url" // 替换为你的Angular应用程序的URL
            },
            ...
        }
    };
}

请确保将上述代码中的http://your-angular-app-url替换为你的Angular应用程序的URL。

  1. 重新启动Angular应用程序和ASP.NET API项目,然后尝试连接到SignalR Hub。

希望上述解决方法能帮助到你解决问题!

相关内容

热门资讯

透视苹果版“pokemmo脚本... 透视苹果版“pokemmo脚本辅助器”透视辅助app(果然真的是有挂);1)pokemmo脚本辅助器...
透视辅助!德扑圈有透视吗,德普... 透视辅助!德扑圈有透视吗,德普之星透视辅助插件,wpk教程(有挂详情)1、实时德普之星透视辅助插件开...
透视软件“xpoker辅助工具... 透视软件“xpoker辅助工具”透视辅助插件(都是真的有挂)1、xpoker辅助工具ai辅助优化,x...
透视好友!德普之星辅助工具如何... 透视好友!德普之星辅助工具如何设置,德普之星怎么开辅助,技巧教程(有挂介绍);1、起透看视 德普之星...
透视能赢!哈糖大菠萝挂法,智星... 透视能赢!哈糖大菠萝挂法,智星菠萝有挂吗,规律教程(有挂黑科技);哈糖大菠萝挂法辅助器中分为三种模型...
透视ai代打!德普之星辅助工具... 透视ai代打!德普之星辅助工具如何设置,如何下载德普之星辅助软件,黑科技教程(有挂方法)1、起透看视...
透视有挂“拱趴大菠萝作弊方法”... 透视有挂“拱趴大菠萝作弊方法”透视辅助工具(都是存在有挂)1、超多福利:超高返利,海量正版游戏,拱趴...
透视ai代打!德普之星透视,德... 透视ai代打!德普之星透视,德扑圈有透视吗,扑克教程(有挂工具)1、起透看视 德扑圈有透视吗透明视辅...
透视黑科技!哈糖大菠萝怎么挂,... 透视黑科技!哈糖大菠萝怎么挂,pokemmo脚本,线上教程(有挂详情)1、打开软件启动之后找到中间准...
透视了解!德普之星透视辅助软件... 透视了解!德普之星透视辅助软件激活码,(德普)原来存在有挂,安装教程(有挂解密);1)德普之星透视辅...