C# .NET Core 3 API模型绑定复杂类型,带10
我有一个Angular 10应用程序,它调用.NET Core 3 API。我有一个复杂的类型,我正试图在一篇文章上建模绑定,但遇到了一个问题。在角度方面,我有一个带有数组的类型,在.NET方面,我试图绑定到的类型包括一个列表。我试图让数组与列表绑定。文章中的数组值是正确的,但我在.NET端的控制器中遇到了错误,特别是在尝试绑定时遇到了参数。下面是我收到的错误以及我的代码 错误 “错误请求状态:400” “无法将JSON值转换为 App.Models.VideoCategoryModel[]” 角度类型C# .NET Core 3 API模型绑定复杂类型,带10,c#,.net,angular,asp.net-core-webapi,asp.net-core-3.1,C#,.net,Angular,Asp.net Core Webapi,Asp.net Core 3.1,我有一个Angular 10应用程序,它调用.NET Core 3 API。我有一个复杂的类型,我正试图在一篇文章上建模绑定,但遇到了一个问题。在角度方面,我有一个带有数组的类型,在.NET方面,我试图绑定到的类型包括一个列表。我试图让数组与列表绑定。文章中的数组值是正确的,但我在.NET端的控制器中遇到了错误,特别是在尝试绑定时遇到了参数。下面是我收到的错误以及我的代码 错误 “错误请求状态:400” “无法将JSON值转换为 App.Models.VideoCategoryModel[]”
export class Video {
constructor(public id?: number,
public title?: string,
public date?: string,
public description?: string,
public url?: string,
public enabled?: boolean,
public categories?: VideoCategory[]){ }
}
export class VideoCategory {
constructor(public id?: number,
public name?: string){ }
}
下面是发送到API的JSON
{
"title":"Test Title",
"date":"2020-12-09",
"description":"Test Description",
"url":"C:\\fakepath\\Capture2.JPG",
"categories":{
"id":101,
"name":"Platform Tutorials"
}
}
.NET核心控制器代码
模型
public class VideoModel
{
public VideoModel()
{
Categories = new List<VideoCategoryModel>();
}
public int Id { get; set; }
public DateTime Date { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string VideoUrl { get; set; }
public bool Enabled { get; set; }
public List<VideoCategoryModel> Categories { get; set; }
}
public class VideoCategoryModel
{
public int Id { get; set; }
public string Name { get; set; }
}
公共类视频模型
{
公共视频模型()
{
类别=新列表();
}
公共int Id{get;set;}
公共日期时间日期{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共字符串VideoUrl{get;set;}
已启用公共bool的{get;set;}
公共列表类别{get;set;}
}
公共类VideoCategoryModel
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
控制器
[HttpPost("addvideo")]
public IActionResult AddVideo([FromBody] VideoModel model)
{
try
{
//var id = _service.AddVideo(_mapper.Map<VideoModel, TaVideo>(model));
//return Ok(id);
return Ok();
}
catch(Exception ex)
{
return BadRequest($"Failed to add the new video: {ex}");
}
}
[HttpPost(“addvideo”)]
公共IActionResult添加视频([FromBody]视频模型)
{
尝试
{
//var id=_service.AddVideo(_mapper.Map(model));
//返回Ok(id);
返回Ok();
}
捕获(例外情况除外)
{
return BadRequest($”添加新视频失败:{ex}”);
}
}
如有任何建议,将不胜感激。很明显,我遗漏了一些东西,或者需要重构绑定的方式,但我不知道最好的方法。提前谢谢
“无法将JSON值转换为App.Models.VideoCategoryModel[]”
根据您的VideoModel
类,我们可以发现Categories
属性应该是VideoCategoryModel
对象的集合。正如@MohsinMehmood在评论中提到的,您应该在您的角度前端代码上为categories字段提供一个数组
"categories":[{
"id":101,
"name":"Platform Tutorials"
}]
此外,请注意,您正在url
字段中传递文件路径,但该属性在模型类中命名为VideoUrl
,为了使模型绑定能够正常工作,您可以尝试在序列化和反序列化以下代码段时指定JSON中存在的属性名称
[JsonPropertyName("Url")]
public string VideoUrl { get; set; }
您能否共享发布到.net核心API的完整json?看起来json无效。它应该是这样的
categories:[{“id”:101,“name”:“Platform Tutorials”},{“id”:102,“name”:“Platform Tutorials 2”}]
@MohsinMehmood-我已经更新了这个问题,以包括发送到API的JSON。看起来我可能缺少括号了?[]嗨@jayh,有关于这个案子的最新消息吗?如果答案解决了您的问题,请接受它-参见