简体   繁体   English

发布的参数在 .NET Core 3.0 API 中出现空白

[英]Posted parameter comes up blank in .NET Core 3.0 API

I created ASP.net core 3.0 web API application and added HttpPost endpoint to it.我创建了 ASP.net core 3.0 web API 应用程序并添加了HttpPost端点。

When I post using postman to this post endpoint, the endpoint doesn't get the JSON I pass to it and instead gets null.当我使用 postman 发布到此发布端点时,端点没有得到我传递给它的 JSON,而是得到 null。

Is there something that has changed in .NET Core 3.0 that has changed/broken HTTP post endpoints? .NET Core 3.0 中是否发生了一些变化,这些变化/破坏了 HTTP 后端点?

The JSON I posted:我发布的 JSON:

{
  "status": "0",
  "operation":"",
  "filter":"",
  "currentOrderList": [
  ]
}

Controller code: Controller 代码:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET: api/<controller>
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

    // POST api/<controller>
    [HttpPost]
    public void Post([FromBody]string value)
    {
    }

    // PUT api/<controller>/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/<controller>/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
}

The url I am posting to is https://localhost:44336/api/values .我要发布的 url 是https://localhost:44336/api/values I can see that the endpoint is being hit by the fact that the method is being hit during debugging in visual studio.我可以看到端点受到了在Visual Studio中调试期间该方法被命中的事实。 The only issue is that the parameter is coming in as null唯一的问题是参数以 null 的形式出现

Create a model to match the given data创建一个 model 以匹配给定的数据

public class MyClass {
    [JsonProperty("status")]
    public int Status { get; set; }
    [JsonProperty("operation")]
    public string Operation { get; set; }
    [JsonProperty("filter")]
    public string Filter { get; set; }
    [JsonProperty("currentOrderList")]
    public string[] CurrentOrderList { get; set; }
}

Then update the controller action to expect the desired type然后更新 controller 操作以期望所需的类型

//POST api/values
[HttpPost]
public IActionResult Post([FromBody]MyClass value) {
    if(ModelState.IsValid) {
        //...
        return Ok();
    }
    return BadRequest();
}

In addition to adding the [JsonProperty] annotations as Nkosi said, I also had to add the nuget package除了按照 Nkosi 所说添加[JsonProperty]注释外,我还必须添加 nuget package

Microsoft.AspNetCore.Mvc.NewtonsoftJson Microsoft.AspNetCore.Mvc.NewtonsoftJson

and append .AddNewtonsoftJson() into Startup.cs.和 append .AddNewtonsoftJson()到 Startup.cs。 I found that in another stackoverflow question, but neither change by itself was enough to get my models to hydrate.我在另一个 stackoverflow 问题中发现了这一点,但是这两个变化本身都不足以让我的模型水合。 It took both to get it working.两者都让它工作。

services.AddMvc().AddRazorRuntimeCompilation().AddNewtonsoftJson();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM