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.
Obviously I am missing something here. TIA Mark
