1
I am a few days beating head with this problem I have a fronted MVC5 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 Save method of the Webapi 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 tried to put before the object [Frombody] and [Fromuri] and nothing. my problem is sending objects to the URI to save or edit on order. When I call a JSON object listing for my frontend it’s OK. But to send not with you.
have I used
httpClient.Postasjsonasync httpClient.Postasync
If anyone can help me! I’m grateful!
Hugs!