Brad8118
Brad8118

Reputation: 4702

Webapi Post has null values

I'm calling a webApi from another project (C# class) and one of the properties is always null. I verified while debugging that the object is properly populated. The object being pass is stored in a shared library that's referenced in both projects. So there shouldn't be a mismatch in types.

I verify before the post is sent that order.Products.Answers is set. then I have a break point in the WebApi Method and its null. I'm not sure why.

Here's the code used to generate the post:

    [HttpPost]
    public ActionResult Add(ApiOrder order)
    {
        try
        {
            HttpResponseMessage resp = null;
            ApiOrder processedOrder = null;

            using (var client = new HttpClient())
            {
                var url = GetUrlToWebApis();
                client.BaseAddress = new Uri(url);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                Task<HttpResponseMessage> task = client.PostAsJsonAsync("apisecure/order", order);
                //resolve the reque
                resp = task.Result;
                var a = resp.Content.ReadAsAsync<ApiOrder>();
                processedOrder = a.Result;
                TempData["ConfirmMessage"] = "Order Was added";
            }

            return View("Add", processedOrder);
        }
        catch (Exception e)
        {
            //ViewBag.messgae = e.InnerException;
            TempData["ErrorMessage"] = e.InnerException;
            return View("Add", order);
        }
    }

And here is the WebApi Method that handles the Post

public IHttpActionResult Post(ApiOrder order)

Here's the class structure of ApiOrder

 [DataContract]
    [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public class ApiOrder
    {
        [DataMember]
        [Required]
        public virtual int Id { get; set; }

        [DataMember]
        // [Required]
        public virtual List<ApiProduct> Products { get; set; }
}

Now the ApiProduct Class

    [DataContract]
    [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public class ApiProduct
    {
        [DataMember]
        [Required]
        public virtual int ActionId { get; set; }
        [DataMember]
        public virtual List<ApiAnswer> Answers { get; set; } // <-- Always null
}

And lastly the ApiAnswer

[DataContract]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class ApiAnswer
{
    [DataMember]
    [Required]
    public virtual int QuestionId { get; set; }

    [DataMember]
    public virtual string QuestionText { get; set; }

    [DataMember]        
    public virtual string Answer { get; set; }
}

Upvotes: 0

Views: 398

Answers (1)

Brad8118
Brad8118

Reputation: 4702

Ahhhhh, The issue was that I have two solutions, and one of the bins had an out of date dll. Cleaning and rebuilding both solutions fixed the problem.

Upvotes: 1

Related Questions