SignalR throws 405 error on notify/negotiate request

╄→尐↘猪︶ㄣ 提交于 2019-12-14 02:34:05

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/52513572/signalr-throws-405-error-on-notify-negotiate-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!