我用这样的角度连接到服务器:
this._hubConnection = new signalR.HubConnectionBuilder()
.withUrl(location.protocol + '//localhost:5000/notify')
.configureLogging(signalR.LogLevel.Information)
.build();
代码或主机都没有问题。该请求有效。
但随后图书馆又对以下内容进行了额外调用:
http://localhost:5000/notify/negotiate
然后我收到这个错误:
HTTP ERROR 405
客户端中的完整错误是:
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.
除了上述要求之外,我在网站上没有 cors 问题。
我在startup.cs中配置了这个。
app.UseCors(builder =>
builder.WithOrigins("http://localhost:4200").AllowAnyMethod
().AllowAnyHeader().AllowAnyOrigin());
该代码适用于所有站点。
如果其余请求有效,为什么会突然出现 cors 错误?
最佳答案
我可以建议您直接配置您的 webapp 的 web.config 文件,添加以下代码行(它适用于 IIS,不确定是否适用于 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>
关于.net-core - SignalR 在通知/协商请求上抛出 405 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52513572/