public ActionResult Search(string name) { string ApiUrl = "https://api.nutritionix.com/v1_1/search/" + name + "?fields=" + "item_name%2Cnf_calories%2Cnf_total_fat&appId=" + Globals.APP_ID + "&appKey=" + Globals.APP_KEY; System.Net.WebRequest req = System.Net.WebRequest.Create(ApiUrl); //req.Proxy = new System.Net.WebProxy(ProxyString, true); //true means no proxy System.Net.WebResponse resp = req.GetResponse(); System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()); string response = sr.ReadToEnd().Trim(); JavaScriptSerializer js = new JavaScriptSerializer(); var d = js.Deserialize<dynamic>(response); Ingredient i; try{ i = new Ingredient() { Name = d["hits"][0]["fields"]["item_name"], Calories = d["hits"][0]["fields"]["nf_calories"], Fat = d["hits"][0]["fields"]["nf_total_fat"] }; } catch { i = new Ingredient(); } return RedirectToAction("RefreshCreate", new { name = i.Name, calories = i.Calories, fat = i.Fat }); }
public void AddIngredient(Ingredient ingredient) { context.AttachRange(ingredient); context.SaveChanges(); }