【问题标题】:Creating a Post Route with ASP.NET CORE 2.0 WebAPI parameter is null使用 ASP.NET CORE 2.0 WebAPI 参数创建发布路由为空
【发布时间】:2019-04-07 20:21:44
【问题描述】:

我正在从 PostMan 发送 POST: 并且参数不断出现为空。根据我的阅读,更改Post([FromBody]Models.Question value) 并设置模型应该能够处理传入的json 参数。我想我缺少一个设置或者我不明白如何正确处理json 数据。

QuestionsController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace quiz_backend.Controllers
{
    [Produces("application/json")]
    [Route("api/Questions")]
    public class QuestionsController : Controller
    {
        // POST api/values
        [HttpPost]
        public void Post([FromBody]Models.Question value)
        {

        }
    }
}

型号是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace quiz_backend.Models
{
    public class Question
    {
        public string Text{ get; set; }
    }
}

【问题讨论】:

  • 在您的请求正文中,您的请求有效负载似乎是{"test":"test"}。应该是{"Text": "text sample"} in order to match the object property Text`。

标签: c# asp.net asp.net-core asp.net-core-2.0 asp.net-web-api-routing


【解决方案1】:

您模型上的属性是Text,而您发送的请求正文属性是"test"。难怪他们不会绑定,你会得到null。大小写在这里无关紧要,但你有不同的词。

【讨论】:

  • 谢谢!!我在晚上工作,完全错过了!
猜你喜欢
  • 1970-01-01
  • 2019-09-29
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 2015-11-13
  • 2017-06-26
相关资源
最近更新 更多