C# Asp.Net核心WebApi无服务器应用程序未正确传递Url值
我在AspNetCoreServer.APIGatewayProxyFunction(AWS lambda函数路由到WebApi)后面有一个控制器,控制器中有以下操作方法:C# Asp.Net核心WebApi无服务器应用程序未正确传递Url值,c#,aws-lambda,asp.net-core-webapi,C#,Aws Lambda,Asp.net Core Webapi,我在AspNetCoreServer.APIGatewayProxyFunction(AWS lambda函数路由到WebApi)后面有一个控制器,控制器中有以下操作方法: [HttpGet("{id}")] public async Task<MyClass> Get(ulong tagId) { Amazon.Lambda.Core.LambdaLogger.Log($"Get(tagId) is called with {tagId}"); return new
[HttpGet("{id}")]
public async Task<MyClass> Get(ulong tagId)
{
Amazon.Lambda.Core.LambdaLogger.Log($"Get(tagId) is called with {tagId}");
return new MyClass { Id = tagId, Description = "Something" };
}
浏览器中的输出:
{
Id: 0,
Description: "Something",
}
Cloudwatch:
Get(TagId) is called with 0
我在VisualStudio中使用AWS工具包进行了部署(也尝试了dotnet lambda CLI,没有任何区别)。我已经在API网关控制台中部署了API。还有另一个操作方法返回所有值(没有传递给该方法的参数),这个方法很好,只有这个带有输入的操作方法无法读取值
在my Startup.cs中:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public static IConfiguration Configuration { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
使用“AWS.NET模拟Lambda测试工具”在本地运行此代码,结果相同,Id将为0
有人能帮我吗?我的想法快用完了 将
tagId
从ulong
更改为long
,也可以将其用作路线约束
确保参数名称和路由模板参数名称匹配
操作中没有等待代码,因此不需要执行异步任务
[Route("api/MyController")]
public class MyController {
//GET api/MyController/1000
[HttpGet("{tagId:long}")]
public MyClass Get(long tagId) {
Amazon.Lambda.Core.LambdaLogger.Log($"Get(tagId) is called with {tagId}");
return new MyClass { Id = tagId, Description = "Something" };
}
}
模型绑定器很可能无法绑定ulong
参数
将
tagId
从ulong
更改为long
,也可以将其用作路线约束
确保参数名称和路由模板参数名称匹配
操作中没有等待代码,因此不需要执行异步任务
[Route("api/MyController")]
public class MyController {
//GET api/MyController/1000
[HttpGet("{tagId:long}")]
public MyClass Get(long tagId) {
Amazon.Lambda.Core.LambdaLogger.Log($"Get(tagId) is called with {tagId}");
return new MyClass { Id = tagId, Description = "Something" };
}
}
模型绑定器很可能无法绑定ulong
参数
为什么cloud watch会有
TagId
?好主意。在粘贴到这里之前,我已经重命名了变量。我现在编辑了这个问题,以反映确切的命名。真正的参数名是tagId,这解释了CloudWatch中的日志。路由模板中的情况如何<代码>{id}这是最初的问题吗?是的。因此,我可以选择让{tagId}匹配动作方法签名,或者重命名动作方法中的参数以匹配{id}。为什么cloud watch会有tagId
?很好。在粘贴到这里之前,我已经重命名了变量。我现在编辑了这个问题,以反映确切的命名。真正的参数名是tagId,这解释了CloudWatch中的日志。路由模板中的情况如何<代码>{id}这是最初的问题吗?是的。因此,我可以选择让{tagId}匹配操作方法签名,或者重命名操作方法中的参数以匹配{id}谢谢!,我将参数重命名为Id,与{Id}匹配,这就成功了。我假设Asp.net core也会按参数数据类型匹配,但我错了。模型绑定器按名称匹配参数,因此它们需要匹配绑定器才能填充它。谢谢!,我将参数重命名为Id,与{Id}匹配,这就成功了。我假设Asp.net core也会按参数数据类型匹配,但我错了。模型绑定器按名称匹配参数,因此它们需要匹配绑定器才能填充它。