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

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

问题描述

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

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

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

但随后库会额外调用:

http://localhost:5000/notify/negotiate

然后我得到这个错误:

<块引用>

HTTP 错误 405

客户端的完整错误是:

<块引用>

未能加载 http://localhost:5000/notify/negotiate: Response to

预检请求未通过访问控制检查:否请求中存在Access-Control-Allow-Origin"标头资源.Origin 'http://localhost:4200' 因此是不允许的访问.

除了上述要求之外,我在网站上没有任何 cors 问题.

我在 startup.cs 中配置了这个.

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

该代码适用于所有网站.

如果其余请求有效,为什么会突然出现 cors 错误?

解决方案

我可以建议你直接配置你的 webapp 的 web.config 文件,添加以下代码行(它适用于 IIS,不确定是否适用于 IIS Express):

<http协议><customHeaders><add name="Access-Control-Allow-Origin";值=http://localhost:4200";/><add name="Access-Control-Allow-Methods";值=GET,POST,OPTIONS,PUT,DELETE,PATCH";/><add name="Access-Control-Allow-Headers";值=内容类型,X-请求的";/>

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.

But then the library makes an additional call to:

http://localhost:5000/notify/negotiate

Then I get this error:

HTTP ERROR 405

The full error in the client is:

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

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.

I have this configured in the startup.cs.

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

The code works for all the site.

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

解决方案

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" />
      </customHeaders>
   </httpProtocol>
</system.webServer>

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

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