Flutter web调用.net核心应用程序接口错误‘访问-控制-允许-原点’
帮助我,我想要flutter-Web调用api
我使用.net核心应用编程接口
如何在.net核心接口上允许‘访问-控制-允许-起源’
Startup.cs中的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace test_api
{
public class Startup
{
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors(MyAllowSpecificOrigins);
// app.UseResponseCaching();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}发布于 2020-09-21 16:06:22
该错误表示您的api (与flutter无关)由于CORS策略而阻止了该请求。CORS会屏蔽其他IP的请求。因此,您几乎必须启用它。
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});遵循指南和答案here。尝试搜索您的特定asp.net核心版本。此外,控制器顶部的EnableCors属性是工作所必需的。您的“仅允许特定来源”根本不指定任何类型的主机。它只指定策略的名称,而且您还没有在services中使用过
https://stackoverflow.com/questions/63988074
复制相似问题