ASP.Net Web API showing correctly in VS but giving HTTP500

前端 未结 7 876
离开以前
离开以前 2020-12-17 04:55

after a lot of help yesterday, I came up against a known error in asp.net4 beta - I upgraded to VS2012 RC Express (4.5), and now I\'m getting an internal server error, and I

7条回答
  •  無奈伤痛
    2020-12-17 05:06

    [Update]

    1, change the action code to include the navigation property data:

        // GET api/Bookings
        public IEnumerable GettblCustomerBookings()
        {
            return db.tblCustomerBookings.Include("tblRentals").AsEnumerable();
        }
    

    2, turn off proxy in data context

    EF suggests to turn off proxy when serializing POCO.

    If you want to support proxy, there are different ways for different serializers:

    JSON.net serializer: It can support proxy by upgrading to latest version. Version 4.5.1 has a bug that can't support ignore NonserializedAttribute. It will block proxy to be serialized.

    DataContractSerializer (JSON/XML): use ProxyDataContractResolver to resolve the types, here is a walkthrough.

    3, enable preserving reference in model class

    Both json.net and DataContract serializer support detecting circular reference and they give developer to control how to handle it.

    Change the model class to following:

    [JsonObject(IsReference = true)]
    [DataContract(IsReference = true)]
    public class tblCustomerBooking
    {
        [Key()]
        public int customer_id { get; set; }
        [DataMember]
        public string customer_name { get; set; }
        [DataMember]
        public string customer_email { get; set; }
        [DataMember]
        public virtual ICollection tblRentals { get; set; }
    }
    
    
    public class tblRental
    {
        [Key()]
        public int rental_id { get; set; }
        public int room_id { get; set; }
        public DateTime check_in { get; set; }
        public DateTime check_out { get; set; }
        public decimal room_cost { get; set; }
        public int customer_id { get; set; }
        [ForeignKey("customer_id")]
        public virtual tblCustomerBooking tblCustomerBooking { get; set; }
    }
    

    Note that, if the model is attributed with DataContract, you have to specify DataMember for all its members, otherwise none of them will be serialized.

提交回复
热议问题