我一直在尝试创建一个简单的 API,我设法使Get
正常工作,但每当我尝试使用Post
或Put
时,它就无法正常工作。
我正在尝试将 JSON 发布/放置,并在我的控制器中将其作为字符串获取。
我正在使用 Postman 和 Insomnia 进行测试(我明确指出,我关闭了两者的 SSL 验证,因为我在本地运行)。
这是我的控制器:
[Route("backoffice/[controller]")]
[ApiController]
public class AddQuestionController : ControllerBase
{
private IQuestionRepository _questionRepository;
public AddQuestionController(IQuestionRepository questionRepository)
{
_questionRepository = questionRepository ?? throw new ArgumentNullException(nameof(questionRepository));
}
[ProducesResponseType((int)System.Net.HttpStatusCode.OK)]
[HttpPost]
public async Task<ActionResult> AddQuestion([FromBody] string question)
{
Question q = JsonConvert.DeserializeObject<Question>(question);
await Task.Run(() => _questionRepository.InsertOne(q));
return Ok();
}
}
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|a0b79872-4e41e975d19e251e.",
"errors": {
"$": [
"The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
]
}
}
所以我认为这是因为在Postman中使用的Json
格式。但是当我尝试使用text
格式时,发生了这种情况:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "|a0b79873-4e41e975d19e251e."
}
每次都没有达到我的控制器的第一行,有人能告诉我这里出了什么问题吗?是我的控制器的问题吗?还是我的Postman使用方式有问题?
。Employee对象类具有以下属性
public int Id { get; set; }其中
EmployeeSkillMiddleTables`是多对多关系中间表的表示。...->继续下一个评论 - noruk"errors": { "$.employeeSkillMiddleTables[0]": [ "
The JSON value could not be converted to System.Collections.Generic.List1[Models.EmployeeSkillMiddleTable]. Path: $.employeeSkillMiddleTables[0] | LineNumber: 3 | BytePositionInLine: 35." ]` 请问如何将这些值传递给端点? - noruk