Flutter 颤振web调用.net核心api错误';访问控制允许原点';
颤振对.net核心api的web调用错误“访问控制允许源” 帮助我我想要颤振Web调用api 我使用.net核心api 如何在.net核心api上允许“访问控制允许源” Startup.cs中的代码Flutter 颤振web调用.net核心api错误';访问控制允许原点';,flutter,asp.net-core,Flutter,Asp.net Core,颤振对.net核心api的web调用错误“访问控制允许源” 帮助我我想要颤振Web调用api 我使用.net核心api 如何在.net核心api上允许“访问控制允许源” Startup.cs中的代码 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.A
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();
});
}
}
}
该错误意味着由于CORS策略,您的api(与颤振无关)已阻止请求。CORS阻止来自其他IP的请求。所以你必须启用它
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
遵循指导原则和答案。尝试搜索特定的asp.net核心版本。此外,还需要控制器顶部的EnableCors
属性才能工作。您的“仅允许特定来源”根本不指定任何类型的主机。它仅指定策略的名称,而且您还未在服务中使用