I have a few days of headaches with this problem I have an MVC5 fronted application and it sends an object to another WebAPI application in a Save () method.
See the code of my Frontend application that sends the object to the WebAPI
public bool Add(UsuarioDto model)
{
if (ModelState.IsValid)
{
using (var httpClient = new HttpClient())
{
var serializedObject = JsonConvert.SerializeObject(model,Formatting.Indented);
var httpContent = new StringContent(serializedObject, System.Text.Encoding.UTF8, "application/json");
var uriWebApi = String.Concat(UriBase, "Usuario/Save");
var response = httpClient.PostAsJsonAsync(uriWebApi, serializedObject).Result;
if (response.IsSuccessStatusCode)
{
var task = response.Content.ReadAsAsync<UsuarioDto>();
task.Wait();
model = task.Result;
}
}
return true;
}
else
{
TempData["Model"] = model;
return false;
}
}
And here is the WebAPI Save method that receives the frontend object it arrives as empty or null.
[HttpPost]
public UsuarioDto Save(UsuarioDto model)
{
try
{
var usuarioDomain = new Usuario();
usuarioDomain.SaveAsync(model);
return (model);
}
catch
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}
I've tried putting before the object [FromBody] and [FromUri] and nothing. my problem is being send objects to the URI save or edit in order. When I call an object listing in JSON to my frontend everything is OK. But to send I can not.
I've used it
link
If someone can help me! I'm grateful!
Hugs!