コード例 #1
0
        public async Task <ActionResult> Index(HomeViewModel postModel)
        {
            if (ModelState.IsValid)
            {
                var recipes = new RecipeResponseModel();

                if (postModel.Ingredients != null && postModel.Ingredients.Count > 0)
                {
                    var ingredientList = string.Join(",", postModel.Ingredients.Select(i => i.Label));

                    recipes = await PullRecipes(ingredientList);
                }

                return(PartialView("_Recipes", recipes));
            }

            return(View(postModel));
        }
コード例 #2
0
        public async Task <RecipeResponseModel> PullRecipes(string ingredients)
        {
            var responseModel = new RecipeResponseModel();

            string url = "http://www.recipepuppy.com/api/?i=" + ingredients;

            HttpClient client = new HttpClient();

            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();

                responseModel = JsonConvert.DeserializeObject <RecipeResponseModel>(content);
            }

            return(responseModel);
        }