.NET Core WebAPI迁移至AWS API Gateway配置及模型绑定问题
Hey there! Let's break down your two questions step by step to get your .NET Core WebAPI running smoothly with AWS API Gateway.
1. Fixing the "Blank API" Issue & Configuring WebAPI to Work with API Gateway
The root cause of your empty API Gateway is almost certainly missing Lambda Proxy Integration—this is the key bridge that lets API Gateway forward requests directly to your .NET Core WebAPI, letting your existing routing logic take over. Here's how to set it up correctly:
Confirm your project has the Lambda entry point
When you converted the solution to a serverless project via Visual Studio's AWS Toolkit, it should have generated a class that inherits fromAPIGatewayProxyFunction. If not, add one manually:using Amazon.Lambda.AspNetCoreServer; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace TestApi { public class LambdaEntryPoint : APIGatewayProxyFunction { protected override void Init(IWebHostBuilder builder) { builder.UseStartup<Startup>(); } } }This class translates API Gateway requests into ASP.NET Core-compatible requests, so your controller routes will be recognized.
Enable Lambda Proxy Integration during deployment
When publishing from Visual Studio to AWS:- Select "API Gateway" as the trigger for your Lambda function.
- In the API Gateway configuration, check the box for Use Lambda Proxy integration.
- Leave the API Gateway endpoint type as "HTTP API" or "REST API" (either works with Proxy integration, but HTTP API is lighter for most use cases).
This setting tells API Gateway to pass all request details (path, parameters, body) directly to your Lambda, instead of requiring you to manually define every endpoint in API Gateway.
Validate your Startup configuration
Make sure yourStartup.cshas the necessary middleware to handle routing:public void ConfigureServices(IServiceCollection services) { services.AddControllers(); // Register your business layer dependencies here services.AddScoped<ITestApiManager, TestApiManager>(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); // Critical for recognizing your controller routes }); }Test the flow
After deployment, you can test your endpoint directly via API Gateway's console: send a GET request tohttps://<your-api-id>.execute-api.<region>.amazonaws.com/api/testapi/data/D-001—it should route to yourSomeMethodcorrectly.
2. Model Binding in API Gateway: Use .NET Core's Built-in Functionality
Good news: if you're using Lambda Proxy Integration, you don't need to mess with API Gateway mapping templates at all. The proxy integration automatically converts API Gateway's request format into a format ASP.NET Core understands, so:
- Route parameters (like
{param}in your example) will bind directly to your method parameters, just like in a regular WebAPI. - POST request bodies will bind to your model classes using .NET Core's default model binding (e.g.,
[FromBody] MyModel model). - Query parameters and request headers will also work with attributes like
[FromQuery]or[FromHeader]as expected.
Mapping templates are only necessary if you choose to use a non-proxy integration (manually defining request/response mappings between API Gateway and Lambda), which adds unnecessary complexity. Stick with proxy integration to leverage all the familiar .NET Core model binding features.
内容的提问来源于stack exchange,提问作者ATHER

