- 在appsettings.json添加属性,里面内容填写允许跨域的地址
///允许跨域地址,逗号隔开
"CorsUrls": "http://localhost:8080/,http://127.0.0.1:8080/,http://localhost:8080/0/blogcreate,https://localhost:8080/"
.
2. 在Program.cs文件配置允许跨域
builder.Services.AddCors(options =>
{
options.AddPolicy
(name: "myCors",
builde =>
{
builde.WithOrigins(builder.Configuration.GetValue<string>("corsUrls").Split(","))
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}
);
});
加上代码之后,再use一下就好了
app.UseCors("myCors");