使用 ASP.NET 创建 Post 路由 CORE 2.0 WebAPI 参数为空
Creating a Post Route with ASP.NET CORE 2.0 WebAPI parameter is null
我正在从 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; }
}
}
您模型上的 属性 是 Text
,而您发送的请求正文 属性 是 "test"
。难怪他们不会绑定,你会得到 null
。这里的大小写无关紧要,但你有不同的词。
我正在从 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; }
}
}
您模型上的 属性 是 Text
,而您发送的请求正文 属性 是 "test"
。难怪他们不会绑定,你会得到 null
。这里的大小写无关紧要,但你有不同的词。