azure 已部署的ASP.NET Core网站无法调用已部署的REST API(启用CORS)

goucqfw6  于 2023-08-07  发布在  .NET
关注(0)|答案(1)|浏览(86)

我在Azure(NET6.0)中部署了一个API,它在代码级和Azure配置级启用了CORS,如下图所示

// Add CORS services to the builder
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAll", builder =>
    {
        builder.AllowAnyOrigin()
               .AllowAnyMethod()
               .AllowAnyHeader();
    });
});

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddDbContext<ContactsDbContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("ContactsDB")));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseCors("AllowAll");
app.UseAuthorization();

字符串
下面是Azure中该API

的CORS配置
我还部署了一个ASP网站.Net,它试图调用上面提到的API,但无法正常工作
这些错误与CORS有关,但似乎与我的API无关

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://js.monitor.azure.com/scripts/b/ai.2.min.js. (Reason: CORS request did not succeed). Status code: (null).

<script> source URI is not allowed in this document: “https://js.monitor.azure.com/scripts/b/ai.2.min.js”. sitecore-mgg.azurewebsites.net:1:1
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://eastus-8.in.applicationinsights.azure.com//v2/track. (Reason: CORS request did not succeed). Status code: (null).

Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.


我还需要配置哪些内容才能从已部署的Azure网站调用REST API?这些错误是否与未调用API有关?

mbzjlibv

mbzjlibv1#

控制台区域中显示的由应用程序Insights引起的错误,我已经禁用了它们,因为我并不真正需要它们
站点和API之间缺乏连接似乎是由于硬编码的URL错误造成的


的数据

相关问题