SignalR在通知/协商请求上引发405错误 [英] SignalR throws 405 error on notify/negotiate request

查看:612
本文介绍了SignalR在通知/协商请求上引发405错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这样的角度连接到服务器:

I connect to the server with angular like this:

    this._hubConnection = new signalR.HubConnectionBuilder()
  .withUrl(location.protocol + '//localhost:5000/notify')
  .configureLogging(signalR.LogLevel.Information)
  .build();

代码或主机没有问题。该请求有效。

There is no problem with the code or the host. The request works.

但是随后库再次调用:

http://localhost:5000/notify/negotiate

然后我得到此错误:


HTTP错误405

HTTP ERROR 405

完整错误客户端中的


Failed to load http://localhost:5000/notify/negotiate: Response to 

预检请求的响应未通过访问控制检查:否
'Access- Control-Allow-Origin'标头出现在所请求的
资源上。因此,不允许
访问源' http:// localhost:4200 '。

preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

除了上述要求外,我的网站没有其他损坏问题。

I don't have cors issues in the site apart from the aforementioned request.

我有

     app.UseCors(builder =>
              builder.WithOrigins("http://localhost:4200").AllowAnyMethod
 ().AllowAnyHeader().AllowAnyOrigin());

该代码适用于所有站点。

The code works for all the site.

如果其余请求正常工作,为什么突然出现cors错误?

Why that sudden cors error if the rest of the requests work?

推荐答案

我建议您直接配置web.config webapp的文件,添加以下代码行(可在IIS上运行,不适用于IIS Express):

I can suggest you to configure directly web.config file of your webapp adding following lines of code (it works on IIS, not sure for IIS Express):

<system.webServer>
<httpProtocol>
 <customHeaders>
   <add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
   <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS,PUT,DELETE,PATCH" />
   <add name="Access-Control-Allow-Headers" value="Content-Type, X-Requested-With" />
   <add name="Access-Control-Allow-Credentials" value="true" />
 </customHeader></httpProtocol></system.webServer>

这篇关于SignalR在通知/协商请求上引发405错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆