我目前正在使用React和SignalR。我正在使用.Net Core2.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");
}
});
}发布于 2018-10-23 16:57:23
我一直收到一个404错误,因为它仍然试图转到/negotiate端点,我知道它不再使用
ASP.NET Core SignalR肯定仍然使用/negotiate端点。在预览中,我们使用了一个OPTIONS请求,但是这导致了很多问题,所以我们回到了/negotiate端点。
看起来您使用的是“开发服务器”(启动时使用spa.UseReactDevelopmentServer)。这通常意味着开发服务器从运行ASP.NET核心应用程序的不同服务器(而不仅仅是由ASP.NET核心应用程序提供的静态文件)中为您的HTML/JS内容提供服务。如果是这样的话,您需要在连接时使用完整的ASP.NET核心服务器进行引用。
这是因为您的HTML/JS内容是由http://localhost:X的开发服务器提供的,而您的ASP.NET核心服务器则运行在http://localhost:Y上。因此,当您使用/ChatHub作为URL时,浏览器将其解释为http://localhost:X/ChatHub,因此您不会访问您的ASP.NET核心应用程序(使用SignalR服务器),而是使用开发服务器,该服务器在该URL上没有内容并生成404。
https://stackoverflow.com/questions/52952157
复制相似问题