相关疑难解决方法(0)

FromBody字符串参数给出null

这可能是非常基本的东西,但我无法弄清楚我哪里出错了.

我试图从POST的主体中获取一个字符串,但"jsonString"只显示为null.我也想避免使用模型,但也许这是不可能的.我用PostMan打的那段代码是这个块:

[Route("Edit/Test")]
[HttpPost]
public void Test(int id, [FromBody] string jsonString)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

也许这是我对邮递员做错的事情,但我一直试图在身体的价值部分使用"= test"(如在关于这个主题的其他问题中看到的那样) - x-www-form-urlencoded section with密钥作为jsonString而没有.我也尝试过使用raw-text和raw-text/plain.我得到了身份证,所以我知道网址是正确的.任何有关这方面的帮助将不胜感激.

PostMan目前设置如下:

POST http://localhost:8000/Edit/Test?id=111
key = id  value = 111
Body - x-www-form-urlencoded
key = jsonString  value = "=test"
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api asp.net-web-api-routing asp.net-web-api2 postman

30
推荐指数
9
解决办法
8万
查看次数

JSON 值无法转换为 System.Nullable[System.Int32]

我将 ASP.NET Core 2.2 API 更新为 ASP.NET Core 3.0,并且我正在使用 System.Json:

services
  .AddMvc()
  .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
  .AddJsonOptions(x => {}) 
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用 Angular 8 发布 JSON 数据,它之前工作过:

{
  "name": "John"
  "userId": "1"
}
Run Code Online (Sandbox Code Playgroud)

ASP.NET Core 3.0 API 中的模型是:

public class UserModel {
  public String Name { get; set; }
  public Int32? UserId { get; set; } 
}
Run Code Online (Sandbox Code Playgroud)

API 控制器动作如下:

[HttpPost("users")]
public async Task<IActionResult> Create([FromBody]PostModel) { 
}
Run Code Online (Sandbox Code Playgroud)

当我提交模型时,出现以下错误:

The JSON value could not be converted to System.Nullable[System.Int32]. 
Run Code Online (Sandbox Code Playgroud)

使用 System.Json 而不是 Newtonsoft 时,我需要做其他事情吗?

c# asp.net-core asp.net-core-3.0 system.text.json

8
推荐指数
2
解决办法
2万
查看次数