C# 当解决方案中有两个项目时,如何为SignalR 3.0指定正确的后端URL?目前获得404
在我的信号器项目中获取此错误:C# 当解决方案中有两个项目时,如何为SignalR 3.0指定正确的后端URL?目前获得404,c#,asp.net-core,signalr,C#,Asp.net Core,Signalr,在我的信号器项目中获取此错误: Failed to load resource: the server responded with a status of 404, 。。。在调用connection.start() 在成功完成之后,我在一个解决方案中使用了两个项目对其进行了修改,一个是后端,一个是前端 后端是一个.NET核心Web API项目 前端是.NET核心Web应用程序 在后端startup.cs文件中,我尝试使用“ChatHub”指定URL,如下所示: app.UseEndpoi
Failed to load resource: the server responded with a status of 404,
。。。在调用connection.start()
在成功完成之后,我在一个解决方案中使用了两个项目对其进行了修改,一个是后端,一个是前端
- 后端是一个.NET核心Web API项目
- 前端是.NET核心Web应用程序
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.MapHub<ChatHub>("/chatHub");
});
var connection = new signalR.HubConnectionBuilder()
.withUrl("https://localhost:44384/chatHub")
.build();
此代码抛出上面列出的错误。我尝试了URL的一些变体,包括。不走运
然后我尝试拆分我的解决方案并分别运行每个项目。修改了我的JavaScript以使用后端端口,如下所示:
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.MapHub<ChatHub>("/chatHub");
});
var connection = new signalR.HubConnectionBuilder()
.withUrl("https://localhost:44384/chatHub")
.build();
但当我检查浏览器时,它显示
Information: Normalizing '/chatHub' to 'https://localhost:*44365*/chatHub'.
注意它使用的端口是错误的端口,它是前端的端口
总结
我相信我接受了一个有效的教程,并仅通过将其分为两个项目对其进行了修改,但我无法找到连接到后端的URL。我尝试了不同的URL,由于某种原因,当我从原始解决方案中分离出后端项目时,它没有使用正确的端口
在完成本教程之后,我修改了它,在一个解决方案中使用了两个项目,一个后端和一个前端
要从单独的signarjavascript客户端应用程序连接到Hub服务器,可以参考以下步骤和代码片段
在中心服务器应用程序(后端,.NET核心Web API项目)中允许跨源信号器连接
在JavaScript客户端上指定集线器的Url,如下所示
测试结果
我按照费汉的建议加上一个新的选项,成功地让它工作起来。此外,您必须以正确的顺序调用UseCors,否则将出现令人沮丧的错误。以下是我的后端Startup.cs中的相关代码:
//This string is for the named policy, discussed in the link above:
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public void ConfigureServices(IServiceCollection services) {
services.AddControllers();
services.AddSignalR();
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder => {
//URLs are from the front-end (note that they changed
//since posting my original question due to scrapping
//the original projects and starting over)
builder.WithOrigins("https://localhost:44323", "http://localhost:51263")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
在Startup.cs中,下面显示了UseCors/UseRouting的重要顺序:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
//Crucial: the next statement must precede UseRouting or you will get an error
//"Response to preflight request doesn't pass access control
//check: No 'Access-Control-Allow-Origin' header is present
//on the requested resource."
app.UseCors(MyAllowSpecificOrigins);
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.MapHub<ChatHub>("/chatHub");
});
}
public void配置(IApplicationBuilder应用程序,IWebHostEnvironment环境){
if(env.IsDevelopment()){
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
//关键:下一条语句必须在UseRouting之前,否则将出现错误
//“对飞行前请求的响应未通过访问控制
//检查:不存在“访问控制允许原点”标题
//在请求的资源上。”
应用程序UseCors(MyAllowSpecificCorigins);
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>{
endpoints.MapControllers();
endpoints.MapHub(“/chatHub”);
});
}
谢谢韩飞,这看起来很有希望。当我从我的公路旅行回来,我会尝试它,希望我可以标记为答案,然后!这让我走上了正确的方向,但还不足以完全解决这个问题。我通过使用您建议的方法,再加上添加Cors策略,成功地实现了这一目标。即使这还不够,在调用app.UseRouting()之前,您必须使用策略调用app.UseCores,这在我阅读的任何文档中都没有出现。表示您的顺序是错误的:“对UseCors的调用必须放在UseRouting之后,但在UseAuthorization之前”
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
//Crucial: the next statement must precede UseRouting or you will get an error
//"Response to preflight request doesn't pass access control
//check: No 'Access-Control-Allow-Origin' header is present
//on the requested resource."
app.UseCors(MyAllowSpecificOrigins);
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.MapHub<ChatHub>("/chatHub");
});
}