代码之家  ›  专栏  ›  技术社区  ›  Thomas Segato

本地PC上从Angular应用程序发布到ASP.NET核心API的CORS

  •  0
  • Thomas Segato  · 技术社区  · 7 年前

    我有一个本地运行的ASP.NET核心API服务和一个本地Angular应用程序。我的大多数调用对API来说都很好,但是一个调用总是给我带来CORS错误。我怀疑是因为这是一个术后手术?根据CORS,帖子需要什么不同的内容吗?

    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;
    
       });
      }
    

    角度服务:

    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);
        }
    

    我有什么东西不见了吗?我想这里什么都是残疾人?在我写这篇文章的时候,我怀疑它和文章有点关系,因为get操作有效。

    邮递员的工作原理是: enter image description here

    我还在控制器上添加了以下内容: [启用CORS(“allowSpecificOrigin”)]

    1 回复  |  直到 7 年前
        1
  •  1
  •   staticvoidmain    7 年前

    有时,

    1)如果API中的请求负载(或)错误中存在错误,chrome开发人员工具控制台将显示CORS。

    请console.log请求并使用postman/swager分别测试API。

    如果1)没有解决问题

    2)有时默认情况下,IIS可以阻止Put和Post操作。请看 this .

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

    这也可能是个问题

    3)我们可能需要向控制器添加[ENABLECORS(“allowSpecificOrigin”)。

    推荐文章