ASP.net REST服务,MS SQL基本序列化列表为null

时间:2018-05-27 13:00:36

标签: asp.net sql-server xml serialization

使用MS SQL服务器作为基础运行ASP.net REST服务。

模型

public class Bookings
{
    [Key]
    public int BookingId { get; set; }

    [Required]
    public int UserID { get; set; }

    public List<string> BookISBN { get; set; }

}

示例生成的文档显示格式为&#34;正确&#34; enter image description here

通过邮递员发送以下数据

{
  "BookingId": 1,
  "UserID": 2,
  "BookISBN": [
    "sample string 1",
    "sample string 2",
    "sample string 3"
  ]
}

然而最后一个清单&#34; BookISBN&#34;未生成列,API将参数返回为NULL / i:nil =&#34; true&#34;

enter image description here

<ArrayOfBookings xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PublisherDataModel">
<Bookings>
<BookISBN xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true"/>
<BookingId>1</BookingId>
<UserID>2</UserID>
</Bookings>
<Bookings>
<BookISBN xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true"/>
<BookingId>2</BookingId>
<UserID>2</UserID>
</Bookings>
</ArrayOfBookings>  

预订的POST控制器。

    // POST api/Bookings
    [ResponseType(typeof(Bookings))]
    public IHttpActionResult PostBooking(Bookings Booking)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Bookings.Add(Booking);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = Booking.BookingId }, Booking);
    }

我缺少什么?

2 个答案:

答案 0 :(得分:0)

您应该使用HttpPost;

装饰您的操作
[HttpPost]
[ResponseType(typeof(Bookings))]
public IHttpActionResult PostBooking(Bookings Booking)

答案 1 :(得分:0)

我必须专门包括/引用它

 db.Entry(Booking).Reference(b => b.BookISBNList).Load();