1

I have just start some R&D on integrating an MVC application that calls a WebApi, both of which I am writing. The WebApi will be called by different applications eventually so I want to have to business logic in here.

I have created the following simple method on my API server:

[HttpPost]
public HttpResponseMessage Get(ShipmentGetCarrierModel view)
{
    var response = Request.CreateResponse<ShipmentGetCarrierModel>(HttpStatusCode.OK, view);
    return response;

    //return new HttpResponseMessage(HttpStatusCode.OK);
}

I am calling this method from my MVC application as follows:

public ActionResult GetOrderNumber2(int orderNumber)
{
    using (var apiServer = new HttpClient())
    {
        apiServer.BaseAddress = new Uri("http://localhost:52126");
        apiServer.DefaultRequestHeaders.Accept.Clear();
        apiServer.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        ShipmentGetCarrierModel model = new ShipmentGetCarrierModel();
        model.nOrderNumber = 100;

        // New code:
        HttpResponseMessage response =
            apiServer.PostAsync("api/PC/Get",
            model,
        new JsonMediaTypeFormatter()).Result;


        if (response.IsSuccessStatusCode)
        {
            return Content(response.Content.ToString());
        }
    }

    return Content("NOT  " + orderNumber.ToString());
}

My model which is identical for both applications is:

public class ShipmentGetCarrierModel
{
    public int nID { get; set; }
    public int nOrderNumber { get; set; }
    public string cConsigneeName { get; set; }
    public string cCarrierCode { get; set; }
    public string cConsigneeAddress1 { get; set; }
    public string cConsigneeAddress2 { get; set; }
    public string cConsigneeCity { get; set; }
    public string cConsigneeProvince { get; set; }
    public string cConsigneePostalCode { get; set; }
    public string cConsigneeTelephone { get; set; }
    public string cConsigneeContact { get; set; }
    public string cConsigneeCountry { get; set; }
    public Boolean? lIsPharmilink { get; set; }
}

I am getting the response code but I cannot see the model data that is returned in the debugger that is created by the WebAPI.

Ironically when I analyse the call using Fiddler, I see my model with their null values.

Fiddler Image

Obviously I am missing something here. TIA Mark

3
  • "I cannot see the model data" Where do you expect to see it but it's not there? Commented Oct 5, 2015 at 14:44
  • Maybe you should consider returning a JsonResult eg : return Json(view, JsonRequestBehavior.AllowGet); Commented Oct 5, 2015 at 14:44
  • I just added the model code above. Commented Oct 5, 2015 at 15:53

1 Answer 1

1

I finally discovered what I was doing wrong; I missed two steps after calling the HttpClient

public ActionResult GetOrderNumber2(int orderNumber)
{
    using (var apiServer = new HttpClient())
    {
        apiServer.BaseAddress = new Uri("http://localhost:52126");
        apiServer.DefaultRequestHeaders.Accept.Clear();
        apiServer.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        ShipmentGetCarrierModel model = new ShipmentGetCarrierModel();
        model.nOrderNumber = 100;

        // New code:
        HttpResponseMessage response =
            apiServer.PostAsync("api/CPC/Get",
            model,
        new JsonMediaTypeFormatter()).Result;

        // I was missing this !!!
        string returnResult = response.Content.ReadAsStringAsync().Result;
        JavaScriptSerializer JSserializer = new JavaScriptSerializer();
        ShipmentGetCarrierModel returnModel = JSserializer.Deserialize<ShipmentGetCarrierModel>(returnResult);


        if (response.IsSuccessStatusCode)
        {
            return Content(returnModel.ToString());
        }
    }

    return Content("NOT  " + orderNumber.ToString());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.