CORS 从 angular 应用到 asp.net 核心 API 在本地电脑上的帖子

CORS with posts from angular app to asp.net core API on local pc

我有一个本地 运行 ASP.NET 核心 API 服务和一个本地 Angular 应用程序。 API 我的大部分通话都很好,但一个电话一直给我 CORS 错误。我怀疑是因为它是一个 post 操作?根据 CORS,posts 是否需要任何不同的东西?

Anuglar ts:

loadGroup(groupName:string){
    this.apiService.getInfluencersWithFilter(this.query,groupName,null,null,this.ageRanges).subscribe((data) => {     
      this.influencerSearchResult = data.results;
      // this.searchGroupsFacet = data.facetGroupList;
      this.searchSubGroupsFacet = data.facetSubGroupList;
      this.showSubGroups = true;

   });
  }

Angular 服务:

getInfluencersWithFilter(q:string, group:string, subGroups:string[], socialAccounts:string[],ageRanges:AgeRange[]):Observable<InfluencerSearchContainer>
{    
        if(q==null)
        {
          q = "";
        }
        var url = `${environment.apiDomain}/api/InfluencersSearch/`;
        return  this.httpClient.post<InfluencerSearchContainer>(url,{q:"",group:group,subGroups:subGroups, socialAccounts:socialAccounts, ageRanges:ageRanges}).pipe(
          map(x => new InfluencerSearchContainer(x)));      
 }

启动ASP.NET核心:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<DocumentClient>((s) =>
        {
            string EndpointUrl = Configuration["CosmosDB:EndpointUrl"];
            string PrimaryKey = Configuration["CosmosDB:PrimaryKey"];
            return new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
        });

        var connStr = Configuration.GetConnectionString("DefaultConnection");
        services.AddDbContext<DB>(options => options.UseSqlServer(connStr));

        services.AddCors(options =>
        {
            options.AddPolicy("AllowSpecificOrigin",
                builder => builder.AllowAnyOrigin()
                       .AllowAnyHeader()
                       .AllowAnyMethod());
        });

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

  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseCors("AllowSpecificOrigin");
        app.UseHttpsRedirection();
        app.UseMvc();
    }

和控制器:

  [HttpPost]
    public InfluencerSearchResultWithFacets Post([FromBody] InfluencerQuery q)
    {
        return GetSearchResult(q.q, q.group,q.subGroups, q.socialAccounts, q.ageRanges);
    }

有什么我遗漏的吗?我认为这里的一切都被禁用了?在我撰写本文时,我怀疑它与 Post 有关,因为获取操作有效。

在 postman 中它有效:

我还在控制器上添加了以下内容: [EnableCors("AllowSpecificOrigin")]

有时候,

1) If there is an error in the request payload (or) error in the api, the chrome developer tools console shows CORS.

请 console.log 请求并分别测试 api 和 postman/swagger。

如果 1) 没有解决问题

2) Sometimes IIS can block PUT and POST operations by default. Please see .

如果 1) 和 2) 没有解决问题

这也可能是个问题

3) We May have to add [EnableCors("AllowSpecificOrigin")] to the controller.