我正在尝试在我的ASP上启用跨起源资源共享。NET核心Web API,但我卡住了。
EnableCors属性接受字符串类型的policyName作为参数:
// Summary:
// Creates a new instance of the Microsoft.AspNetCore.Cors.Core.EnableCorsAttribute.
//
// Parameters:
// policyName:
// The name of the policy to be applied.
public EnableCorsAttribute(string policyName);
policyName是什么意思,如何在ASP上配置CORS。NET核心Web API?
安装nuget包Microsoft.AspNetCore.CORS
在ConfigureServices方法下的Startup.cs中,在services之前添加以下代码。
services.AddCors(options =>
{
options.AddPolicy("AllowMyOrigin", p =>
{
p.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
在Startup.cs的Configure方法中添加app.UseCors("AllowMyOrigin");调用app.UseMvc()之前
注意,当从客户端发送请求时,记得使用https而不是http。
特别是在dotnet核心2.2中,你必须改变SignalR
.WithOrigins (http://localhost: 3000)或
.SetIsOriginAllowed(isOriginAllowed: _ => true) //所有源
用.AllowCredentials()代替。allowanyorigin ()
https://trailheadtechnology.com/breaking-change-in-aspnetcore-2-2-for-signalr-and-cors/
https://github.com/aspnet/AspNetCore/issues/4483
安装nuget包Microsoft.AspNetCore.CORS
在ConfigureServices方法下的Startup.cs中,在services之前添加以下代码。
services.AddCors(options =>
{
options.AddPolicy("AllowMyOrigin", p =>
{
p.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
在Startup.cs的Configure方法中添加app.UseCors("AllowMyOrigin");调用app.UseMvc()之前
注意,当从客户端发送请求时,记得使用https而不是http。