【发布时间】:2019-12-07 00:07:35
【问题描述】:
在 CORS 配置中,我允许使用 OPTIONS 方法,但是当我发送请求时,会抛出错误
选项http://x.x.x.x:port/function 405(方法不允许)
我在 Startup.cs 中的 ConfigureServices 方法中的 CORS 策略如下:
services.AddCors(options =>
{
options.AddPolicy("AllowAny", x =>
{
x.AllowAnyOrigin()
.AllowAnyHeader()
.WithMethods("POST", "OPTIONS");
});
});
它的用法如下:
app.UseCors("AllowAny");
中心代码:
public async Task SendMessage(string user, string message) {
string outputMessage = SensorsControl.controlSensors(message.ToString());
await Clients.All.SendAsync("ReceiveMessage", user, outputMessage);
}
角度的SignalR代码:
this._hubConnection = new HubConnection('http://x.x.x.x:port/function');
this._hubConnection.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error while establishing connection'));
this._hubConnection.on('ReceiveMessage', (user: string, outputMessage: string) =>
{
const text = ${user}: ${outputMessage}; this.messages.push(text);
});
【问题讨论】:
-
你能显示你的信号器代码和你的集线器代码吗?
-
@TonyNgo 还有什么需要的吗?
-
更新你的问题而不是你的评论
-
完成 :) @TonyNgo
-
您也可以尝试在其他地方添加标题。为此,我经常使用 web.config:gist.github.com/Jalalhejazi/5653347
标签: c# angular asp.net-core cors signalr