public HttpResponseMessage AddPresence([FromBody] Presence newPresence) { if (ModelState.IsValid) { int presenceId; try { if (LogicUser.AddPresence(newPresence)) { presenceId = LogicUser.GetCurrentPresenceId(newPresence); return(new HttpResponseMessage(HttpStatusCode.Created) { Content = new ObjectContent <Int32>(presenceId, new JsonMediaTypeFormatter()) }); } } catch (Exception e) { new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new ObjectContent <String>($"Can not add to DB - {e.Message}", new JsonMediaTypeFormatter()) }; } } ; List <string> ErrorList = new List <string>(); //if the code reached this part - the user is not valid foreach (var item in ModelState.Values) { foreach (var err in item.Errors) { ErrorList.Add(err.ErrorMessage); } } return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new ObjectContent <List <string> >(ErrorList, new JsonMediaTypeFormatter()) }); }