我有一个使用 .Net Core 开发的 Web-API。它有几个端点( GET/POST )。要求是将其移动到 AWS API-Gateway。该 Web-API 是使用分层架构构建的,它有一个与 Db 层对话的业务层,该层获得了一些 Entity Framework 存储库(后端数据库 Postgres)。现在我已经将我的解决方案重新创建为 AWS 无服务器解决方案(使用 AWS Toolkit for visual studio 附带的模板项目之一)。
问题是如何启用我的 Web API 方法 AWS API Gatway?我尝试按原样将我的 web-api 发布到 AWS,但它在 api 网关中创建了一个空白 api(Visual Studio 表示已成功发布),这意味着出于某些原因,Api-Gateway 无法在我的解决方案中识别我的端点,我认为原因是我不知道如何正确配置它们并启用 AWS-API 网关......
第二个问题是 模型绑定(bind)如何在 AWS API-GATEWAY 中工作。我应该使用映射模板来实现模型绑定(bind),还是内置的 .net 核心 Web API 模型绑定(bind)可以工作并且足够了?
下面是开发好的Web API示例,需要部署到AWS-API-Gateway
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace TestApi
{
[Route("api/testapi")]
public class TestApiController : Controller
{
private ITestApiManager testApiManager;
public TestApiController(ITestApiManager testApiManager)
{
this.testApiManager = testApiManager;
}
// GET: api/testapi/data/D-001
[HttpGet]
[Route("data/{param}")]
public IActionResult SomeMethod(string param)
{
// This method access busines layer which calls data access layer to get the data from postgress database using entity framework
}
// There are some more similar GET and POST methods in this api as well
}
}
最佳答案
好吧,我正在回答我自己的问题,以防万一其他人正在寻找相同的信息。我的端点在 API 网关上不可用的原因是我的 lambda 处理程序不完全合格,我必须在 serverless.template 文件中配置 Proxy+ 部分。
对于您的 serverless.template 文件,检查 Resources > AspNetCoreFunction > Handler 属性。它应该具有这种格式
"Handler": "<your-web-api-project-name>::<namespace-for-your-lambda>.<lambda-class-name>::FunctionHandlerAsync"
我还必须添加这些以使我的 API 在 AWS Gateway 上工作到 serverless.template
"Events": {
"ProxyResource": {
"Type": "Api",
"Properties": {
"Path": "/{proxy+}",
"Method": "ANY"
}
},
"RootResource": {
"Type": "Api",
"Properties": {
"Path": "/",
"Method": "ANY"
}
}
关于amazon-web-services - 将 .Net Core Web-API 移动到 AWS Web Api Gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57432461/