POST 复杂对象到 ApiController 子类
POST Complex Object to ApiController Subclass
我正在开发一个 API 方法,它应该接受一个 Image
模型对象,它有一个 属性 List<Comment> Comments
。从移动应用程序发布的 Image
工作正常,但如果我包含一个 Comment
对象数组,它们不会显示在 Image
的实例上。我对 C# 不是很好,所以任何帮助将不胜感激。
图片Class
public class Image
{
public int? ImageId { get; set; }
[Required]
public string Image { get; set; }
[Required]
public string ContentType { get; set; }
[Required]
public string Filename { get; set; }
[Required]
public DateTime DateTaken { get; set; }
[Required]
public int UserId { get; set; }
[Required]
public int CompanyId { get; set; }
[Required]
public int LocationId { get; set; }
public decimal? Lat { get; set; }
public decimal? Long { get; set; }
public List<ApiComment> Comments { get; set; }
}
评论Class
public class ApiComment
{
[Required]
public string Comment { get; set; }
[Required]
public DateTime DateCreated { get; set; }
[Required]
public int UserId { get; set; }
}
ImagesController 的开头
public class ImagesController : ApiController
{
[System.Web.Http.HttpPost]
public ActionResult Post(Image image)
我认为您请求的请求正文有问题:
我在 fiddler 中试过这个:
{ "Comments" : [{ "Comment" : "Hello"}, {"Comment" : "World"}]}
我的 WebAPI 操作方法中的 评论 有两个计数。
检查您发布的请求对象是否存在拼写错误,并且 Json 有效。
我正在开发一个 API 方法,它应该接受一个 Image
模型对象,它有一个 属性 List<Comment> Comments
。从移动应用程序发布的 Image
工作正常,但如果我包含一个 Comment
对象数组,它们不会显示在 Image
的实例上。我对 C# 不是很好,所以任何帮助将不胜感激。
图片Class
public class Image
{
public int? ImageId { get; set; }
[Required]
public string Image { get; set; }
[Required]
public string ContentType { get; set; }
[Required]
public string Filename { get; set; }
[Required]
public DateTime DateTaken { get; set; }
[Required]
public int UserId { get; set; }
[Required]
public int CompanyId { get; set; }
[Required]
public int LocationId { get; set; }
public decimal? Lat { get; set; }
public decimal? Long { get; set; }
public List<ApiComment> Comments { get; set; }
}
评论Class
public class ApiComment
{
[Required]
public string Comment { get; set; }
[Required]
public DateTime DateCreated { get; set; }
[Required]
public int UserId { get; set; }
}
ImagesController 的开头
public class ImagesController : ApiController
{
[System.Web.Http.HttpPost]
public ActionResult Post(Image image)
我认为您请求的请求正文有问题:
我在 fiddler 中试过这个:
{ "Comments" : [{ "Comment" : "Hello"}, {"Comment" : "World"}]}
我的 WebAPI 操作方法中的 评论 有两个计数。
检查您发布的请求对象是否存在拼写错误,并且 Json 有效。