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。

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

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...