I have .NET Core Web App with the following controller:
[HttpPost]
public async Task<IActionResult> Update(StudentDetailsViewModel vm)
{
var tokenNo = HttpContext.Session.GetString("Token");
vm.ID = Convert.ToInt32(HttpContext.Session.GetString("StudentId"));
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenNo);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var putStudentUrl = _appSettings.Value.Apis.GSRTCApi.Url + _appSettings.Value.Apis.GSRTCApi.StudentsEndpoint + vm.ID;
var settings = new JsonSerializerSettings();
var stringData = JsonConvert.SerializeObject(vm);
var contentData = new StringContent(stringData, Encoding.UTF8, "application/json");
var response = await client.PutAsync(putStudentUrl, contentData); // contentData);
return RedirectToAction("Index", "Home");
}
The controller calls my Web API and everything works fine until I upload a file through my html form. When this happens the file is picked up in the IFormFile property of the StudentDetailsViewModel on the client side but when the API call is made the whole object is null. The API controller is:
[HttpPut("{id}")]
public async Task<IActionResult> Put(int? id, [FromBody]StudentViewModel student)
{
// API operations here
}
My suspicion is that I am not serializing the StudentDetailsViewModel object properly since I have a property IFormFile, which is an interface. However, I am not sure exactly how I need to customize the Json.Newsoft object.