使用ASP.NET CORE 2.0 WebAPI参数创建发布路由为空

时间:2018-11-04 05:34:32

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

我正在从PostMan发送POST:screenshot of postman settings 并且参数不断出现为空。从我读到的内容来看,更改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; }
    }
}

1 个答案:

答案 0 :(得分:2)

模型上的属性为constructor(props) { super(props); this.state = { isLoading: null, isDeleting: null, list: null, title: "", term: "", content: [] }; } async componentDidMount() { try { const list = await this.getList(); const { title, content } = list; this.setState({ list, title, content }); } catch (e) { alert(e); } } checkedChange = async event => { let todos = Object.assign({}, this.state.content); let key = event.target.attributes[0].value; if(event.target.checked) { todos[key] = true; this.setState({todos}); } else { todos[key] = false; this.setState({todos}); } }; ,而您要发送的请求正文属性为Text。难怪它们不会绑定,您得到"test"。这里的大小写无关紧要,但是您在一起使用的单词完全不同。