public APIResult Post(TravelFood travelFood) { APIResult success = new APIResult() { Success = true, Message = null }; APIResult error = new APIResult() { Success = false, Message = "" }; int num = 0; try { var config = new MapperConfiguration(cfg => { cfg.CreateMap <TravelFood, FavTravelFood>(); }); IMapper mapper = config.CreateMapper(); var source = travelFood; var dest = mapper.Map <TravelFood, FavTravelFood>(source); if (!FavTravelFoodExists(dest.ID)) { db.FavTravelFood.Add(dest); num = db.SaveChanges(); } else { error.Message = "資料已存在"; } } catch (Exception ex) { error.Message = ex.Message.ToString(); } if (num == 1) { return(success); } else { return(error); } }
public async System.Threading.Tasks.Task <APIResult> PostAsync([FromUri] TravelFood travelFood) { APIResult error = new APIResult() { Success = false, Message = "餐館不存在" }; if (travelFood.ID == null) { return(error); } else { if (travelFoods.Where(m => m.ID == travelFood.ID).Any()) { travelFood = travelFoods.Where(m => m.ID == travelFood.ID).Select(o => new TravelFood { ID = o.ID, Name = o.Name }).FirstOrDefault(); APIResult fooAPIResult; using (HttpClientHandler handler = new HttpClientHandler()) { using (HttpClient client = new HttpClient(handler)) { try { #region 呼叫遠端 Web API string FooUrl = $"http://localhost:59439//api/FavProcessService"; HttpResponseMessage response = null; #region 設定相關網址內容 var fooFullUrl = $"{FooUrl}"; // Accept 用於宣告客戶端要求服務端回應的文件型態 (底下兩種方法皆可任選其一來使用) //client.DefaultRequestHeaders.Accept.TryParseAdd("application/json"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Content-Type 用於宣告遞送給對方的文件型態 client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); string username = "******"; string password = "******"; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes($"{username}:{password}"))); var fooJSON = JsonConvert.SerializeObject(travelFood); // https://msdn.microsoft.com/zh-tw/library/system.net.http.stringcontent(v=vs.110).aspx using (var fooContent = new StringContent(fooJSON, Encoding.UTF8, "application/json")) { response = await client.PostAsync(fooFullUrl, fooContent); } #endregion #endregion #region 處理呼叫完成 Web API 之後的回報結果 if (response != null) { if (response.IsSuccessStatusCode == true) { // 取得呼叫完成 API 後的回報內容 String strResult = await response.Content.ReadAsStringAsync(); fooAPIResult = JsonConvert.DeserializeObject <APIResult>(strResult, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); } else { fooAPIResult = new APIResult { Success = false, Message = string.Format("Error Code:{0}, Error Message:{1}", response.StatusCode, response.RequestMessage) }; } } else { fooAPIResult = new APIResult { Success = false, Message = "應用程式呼叫 API 發生異常" }; } #endregion } catch (Exception) { fooAPIResult = new APIResult { Success = false, Message = "應用程式呼叫 API 發生異常"//ex.Message, }; } } } return(fooAPIResult); } else { return(error); } } }