c# - 在 VS2019 中使用 API Gateway 部署到 AWS Lambda 会给出 {"message":"Internal Server Error"}
问题描述
从 Lambda 控制台测试 Lambda 本身;我传入“asdf”并得到“ASDF”作为响应。
但是,当我添加 API 网关时:
- 转到 Lambda 控制台:https ://us-west-2.console.aws.amazon.com/lambda/home
- 添加触发器
- API 网关
- HTTP API(REST API 有同样的问题)
- 打开
- 启用 CORS
我收到 {"message":"Internal Server Error"}
此代码有效(删除input
参数):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Complyify.Contexts;
using Complyify.Engines;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace ComplyifyLambda
{
public class Function
{
public string FunctionHandler(/* string input, */ ILambdaContext context)
{
return "Hello, World";
}
}
}
此代码不起作用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Complyify.Contexts;
using Complyify.Engines;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace ComplyifyLambda
{
public class Function
{
public string FunctionHandler(string input, ILambdaContext context)
{
return input?.ToUpper();
}
}
}
解决方案
正如@zaitsman 指出的,我需要使用Amazon.Lambda.APIGatewayEvents
NuGet 包。
我发现这个答案很有用。
此代码有效:
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
var bodyString = request?.Body;
if (!string.IsNullOrEmpty(bodyString))
{
return new APIGatewayProxyResponse
{
StatusCode = 200,
Body = bodyString.ToUpper()
};
}
return new APIGatewayProxyResponse
{
StatusCode = 200,
Body = "No body!"
};
}
推荐阅读
- node.js - Html-pdf npm 使用命令行将页面大小更改为 300*400
- javascript - NodeJS 高效数据库日期超时事件
- c# - C#如何将游戏对象链接到变换?
- node.js - 有没有办法可以清除这个 npm 错误
- python-3.x - 你好!有人能解释一下为什么这段代码会出现错误“ValueError: too many values to unpack”吗?
- python - 使用 docker、nginx、flask 和 uwsgi 连接到远程 mySQL 数据库
- android - 您如何以及如何为 espresso 创建测试数据?(安卓)
- asp.net-core - 如何将图像文件分配给 ASP.net Core 中的 IFormFile 变量?
- python - 几何形状Python之间的测地线距离
- python - 在 pandas 数据框中对字符串进行分组