c# - 接受 Text/Plain 格式,就像它是 JSON 一样,以便可以解析模型
问题描述
我有一个 API 将特定类型作为输入,但前端发送请求text/plain
而不是application/json
.
我怎样才能使 API 自动接受数据,就好像它是 JSON 一样?
[Route("GetNotification")]
[HttpPost]
public IActionResult GetAllNotifications([FromBody]GetNotificationsBySearchVm Search)
{
try
{
GetAllNotificationsVm result = notificationBusiness.GetAllNotification(Search);
return Ok(result);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
这是请求的正文text/plain
:
{
"index":1 ,
"size": 10,
"userID":26,
"appID": null
}
运行时,我收到此错误:
InvalidCastException:无法将类型的对象转换
System.String
为类型xxx.GetNotificationsBySearchVm
。
解决方案
推荐阅读
- php - 如果使用 php 的 api 验证失败,则显示错误
- python - 忽略无值并跳到下一个值?
- node.js - 给定 HapiJS 服务器对象,如何确定插件是否已注册?
- r - 带有 Flextable 的多行标题
- node.js - Mongoose 将数组值返回为“未定义”
- selenium - 如何从 selenium 中的字段中删除数据
- php - 如何在 get_where 中使用 LIKE
- sql-server - 哪个帐户用于访问数据库(WCF / 无 AD / IntegratedSecurity)?
- docker - 如何从 PHP 中的 wordpress docker 图像写入 STD / docker 日志?
- c# - 由于“AMD64”处理器不匹配,无法在 MSIX 中发布 WPF