c# - Json post int 类型始终为 0

标签 c# asp.net-core .net-core

[HttpPost("method")]
public string Method(int number)
{
    return number.ToString();
}

为什么数字总是0?

可以像这样使用原始类型的 json post 吗?

我的帖子:{ "number": 3 }

最佳答案

如果您发布像 { "number": 3 } 这样的数据,您的操作方法应该是

public class Data
{
   public int Number {get; set;}
}

[HttpPost("method")]
public string Method([FromBody] Data data)
{
    return data.Number.ToString();
}

关于c# - Json post int 类型始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45174143/

相关文章:

c# - 单元测试 : Using another method to check the method under test ran correctly

c# - ObjectDataSource 在嵌套的 gridview 中多次调用

c# - 使用命令行构建 C#/.NET 时如何设置配置?

ubuntu - dotnet build obj/project.assets.json' 已经存在

c# - ASP.NET Core - 依赖注入(inject) - IdentityServer4 DbContext

c# - 具有设计模式统计信息的示例项目

asp.net-core - 从 IdentityServer4 创建的后端删除用户

c# - ASPNET核心2.0 : Is there a way to access IOptions without constructor pattern?

asp.net-mvc - 在脚手架标识后禁用预构建的 Razor UI 页面

Docker 和网络核心应用程序不起作用