我写了个WEBAPI 发布在192.168.1.111:8110 上。
我的WEB程序发布在192.168.1.111:8100上
两个程序端口号不同。但是WEB程序访问WEBAPI的时候提示跨域问题。
在微软官网发现,在WEBAPI中加入下面代码,轻松搞定
builder.Services.AddCors
app.UseCors();
下面是program.cs 完整代码
using CompoundingDeviceWEBAPI.Helper;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(
policy =>
{
policy.AllowAnyOrigin();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();
官网说明