AWS Gateway子分量支持ASP.NET Core

发布于 2025-02-01 17:54:45 字数 1523 浏览 5 评论 0原文

我创建了一个简单的AWS无服务器“ ASP.NET Core Minimal”应用程序,并且一切都按预期工作,因为应用程序(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();

是否有可能以某种方式解释该应用程序现在已绑定到子程序的应用程序?

I have created a simple AWS Serverless "Asp.Net Core minimal" application and everything works as expected because the application(lambda) is bound to the root route of the Api gateway.
Here is the configuration:

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

But if to change the root route to some subroute, for example "/hello",

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

everything stops working and the application constantly returns a 404 error instead of a welcome message.

Here is the application code:

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();

Is it possible somehow to explain for the application that now it is bound to the subroute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2025-02-08 17:54:45

您需要更改功能中的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");

you need to change the url in function. It needs to be like:

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

And if you have any route parameters you can also use

app.MapGet("/hello/{name}", (string name) => "Welcome to running ASP.NET Core Minimal API on AWS Lambda");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文