.Net Core 跨域

.Net Core 2.1

添加依赖项

NuGet安装

Startup.cs添加代码

public void ConfigureServices(IServiceCollection services)
{

 //配置跨域处理

services.AddCors(
options =>
options.AddPolicy("AllowAllOrigin",
builder =>
{

builder.AllowAnyOrigin() //允许任何来源的主机访问
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();//指定处理cookie
})
);

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//使用跨域
app.UseCors("AllowAllOrigin");

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseMvc();
}

猜你喜欢

转载自www.cnblogs.com/thinZ/p/9150992.html