这是我的Post()方法
[HttpPost]
[Consumes("Application/Json")]
public IActionResult Post([FromBody]Album value)
{
if(value==null)
{
return BadRequest("Album cannot be empty");
}
if(value.AlbumName==null)
{
return BadRequest("Album Name cannot be empty");
}
else if(value.AlbumName.Equals(""))
{
return BadRequest("Album Name cannot be empty");
}
if (value.AlbumReleasedate == null||value.AlbumReleasedate.Equals("") )
{
return BadRequest("Album Release Date cannot be empty");
}
if (value.CoverImageURL.Equals("") || value.CoverImageURL == null)
{
return BadRequest("Cover Image URL cannot be empty");
}
return Ok(value);
}
这是专辑类
public class Album
{
public int AlbumId;
public string AlbumName { get; set; }
public string CoverImageURL { get; set; }
public DateTime AlbumReleasedate { get; set; }
public Artist artist;
public List<Song> Songs;
}
这里的value.AlbumName为null。 当我使用POSTMAN使用Body发送此请求时
{
"Album":{
"coverImageURL": "https://en.wikipedia.org/wiki/Let_Go_",
"albumReleasedate": "2002-06-04T00:00:00",
"albumName": "Let Go."
}
}
当我向控制器发送POST请求时,数据不会绑定到我使用过的对象。我如何绑定这些数据?
答案 0 :(得分:1)
{ "coverImageURL": "https://en.wikipedia.org/wiki/Let_Go_", "albumReleasedate": "2002-06-04T00:00:00", "albumName": "Let Go." }