I use SignalR (v2.4.0)
in an ASP.NET MVC
project with Angular 8
and encounter "Access to XMLHttpRequest at 'https:/demo.com/signalr/negotiate?clientProtocol=2.1&connectionData=' from origin 'http://localhost:20700' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." error while debugging (also similar error in PROD stage). I have tried to apply fixes on Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method, but most of them not worked or not a global fix. Instead of applying Controller stage, I would prefer a fix globally in Global.asax
or Startup.cs
(I also tried web.config fixes, but never worked). So, is there any global fix for this problem?
Update: Here is my configuration to fix the problem:
Startup.cs:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(Demo.Web.UI.App_Start.Startup))]
namespace Demo.Web.UI.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
web.config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:20700"/>
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
</system.webServer>