【发布时间】:2019-11-05 19:27:12
【问题描述】:
我在 AspNetCoreServer.APIGatewayProxyFunction(用于路由到 WebApi 的 AWS lambda 函数)后面有一个控制器,在我的控制器中使用此操作方法:
[HttpGet("{id}")]
public async Task<MyClass> Get(ulong tagId)
{
Amazon.Lambda.Core.LambdaLogger.Log($"Get(tagId) is called with {tagId}");
return new MyClass { Id = tagId, Description = "Something" };
}
它正确地路由到这个方法,但它没有传递在 url 中指定为 Id 的值。实际上,问题是“id”总是0。我将方法签名更改为“string id”,但方法总是收到空字符串。
点击 APIGateway 端点(Prod 是舞台):
https://*********.execute-api.*****.amazonaws.com/Prod/api/MyController/1000
在浏览器中输出:
{
Id: 0,
Description: "Something",
}
云观察:
Get(TagId) is called with 0
我已经在 Visual Studio 中使用 AWS 工具包进行了部署(也尝试了 dotnet lambda CLI,没有任何区别)。我已经在 API Gateway 控制台中部署了 API。还有另一种操作方法返回所有值(没有传递给该方法的参数)并且一个很好,只有这个带有输入的操作方法无法读取值。
在我的 Startup.cs 中:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public static IConfiguration Configuration { get; private set; }
// 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);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
使用“AWS .NET Mock Lambda 测试工具”在本地运行此代码结果相同,Id 将为 0。
有人可以帮我吗?我的想法不多了。
【问题讨论】:
-
云表为什么会有
TagId? -
好收获。在粘贴到这里之前,我已经重命名了变量。我现在编辑了问题以反映确切的命名。真正的参数名称是 tagId,它解释了 CloudWatch 中的日志。
-
在路由模板中呢?
{id}是原来的问题吗? -
是的。所以我可以选择让 {tagId} 匹配动作方法签名,或者重命名动作方法中的参数以匹配 {id}
标签: c# aws-lambda asp.net-core-webapi