gpt4 book ai didi

c# - GAE Flexible 不使用 CORS 返回正确的响应 header

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:01 24 4
gpt4 key购买 nike

使用 Google App Engine 运行与 .NET Core API 对话的 Angular 应用。但是,我在使用 CORS 时遇到了问题。

.NET Core API 使用以下 OpenAPI.yaml 在 GAE flexible 上运行:

# [START swagger]
swagger: "2.0"
info:
description: "A simple Google Cloud Endpoints API example."
title: "Endpoints Example"
version: "1.0.0"
host: "myapi.appspot.com"
x-google-endpoints:
- name: "myapi.appspot.com"
allowCors: "true"
# [END swagger]
consumes:
- "application/json"
produces:
- "application/json"
schemes:
- "https"
paths:
"/api/mailinglist":
post:
description: "Echo back a given message."
operationId: "mailinglist"
produces:
- "application/json"
responses:
200:
description: "Echo"
schema:
$ref: "#/definitions/echoMessage"
parameters:
-
description: "Message to echo"
in: body
name: message
required: true
schema:
$ref: "#/definitions/echoMessage"
definitions:
echoMessage:
type: "object"
properties:
message:
type: "string"
authInfoResponse:
properties:
id:
type: "string"
email:
type: "string"

我显然已经将我的实际 API 域更改为 myapi...

以下是正在发送/返回的 header 。对于我的生活,我无法弄清楚为什么根本没有发送 Access-Control-Allow-Origin

enter image description here

最后,我的 API 代码:

public class Startup
{
private 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

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

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseCors(MyAllowSpecificOrigins);

app.UseHttpsRedirection();
app.UseMvc();
}
}

有什么想法吗?

最佳答案

已解决。

在我的 C# 代码中,我忘记添加 AllowAnyMethod()

public class Startup
{
private 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

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

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseCors(MyAllowSpecificOrigins);

app.UseHttpsRedirection();
app.UseMvc();
}
}

关于c# - GAE Flexible 不使用 CORS 返回正确的响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58378500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com