【问题标题】:AWS Gateway subroutes support for Asp.Net CoreAWS Gateway 子路由对 Asp.Net Core 的支持
【发布时间】:2022-07-02 00:01:02
【问题描述】:

我创建了一个简单的 AWS 无服务器“Asp.Net Core 最小”应用程序,一切正常,因为应用程序 (lambda) 绑定到 Api 网关的根路由。 这是配置:

      "Events": {
          "ProxyResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          },
          "RootResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/",
              "Method": "ANY"
            }
          }
        }

但是如果把根路由改成某个子路由,比如"/hello"

     "Events": {
          "ProxyResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/hello/{proxy+}",
              "Method": "ANY"
            }
          },
          "RootResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/hello/",
              "Method": "ANY"
            }
          }
        }

一切都停止工作,应用程序不断返回 404 错误而不是欢迎消息。

这是应用程序代码:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi);
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.MapControllers();
app.MapGet("/", () => "Welcome to running ASP.NET Core Minimal API on AWS Lambda");

app.Run();

是否有可能以某种方式为应用程序解释它现在绑定到子路由?

【问题讨论】:

  • app.MapGet("/hello",... 也许?
  • 这是我尝试过的第一个方法,它可以工作,但在不接触代码的情况下,无法灵活地将 lambda 部署到任何 URL
  • 你不能有一个使用控制器而不检查请求对象的应用程序,映射到动态路由。

标签: c# asp.net-core routes aws-api-gateway


【解决方案1】:

您需要更改函数中的 url。应该是这样的:

app.MapGet("/hello", () => "Welcome to running ASP.NET Core Minimal API on AWS Lambda");

如果你有任何路由参数也可以使用

app.MapGet("/hello/{name}", (string name) => "Welcome to running ASP.NET Core Minimal API on AWS Lambda");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-23
    • 2019-01-06
    • 2021-12-29
    • 2017-05-24
    • 1970-01-01
    • 2019-05-17
    • 2021-09-04
    • 2021-10-13
    相关资源
    最近更新 更多